tags:

views:

126

answers:

5

hello,

How to load content in to a html page. please note IM not allowed to use php or C. Only javascript and html.

for example

load Page B in to Page A

http:myweb.com/index.html?load=pageb

thank you.

A: 

Using jQuery:

 $.ajax({
   type: "POST",
   url: "http://some.com/page.html",
   success: function(msg){
     alert( "here's your data: " + msg );
     jQuery("#yourDivID").html(msg);
   }
 });

http://docs.jquery.com/Ajax/jQuery.ajax

edit: added how to put it into a div

marcgg
+2  A: 
  1. Issue an AJAX request to Page B

  2. Get the contents using responseText

  3. Display the contents inside a div using innerHTML property.

If you can use a js framework then I would suggest jQuery and @marcgg's answer will do it.

rahul
A: 

hello again,

sorry but i don't get it yet. an sample html body sample would be great.

Power-Mosfet
+1  A: 

Just plain JavaScript:

<html>
<head>
<script>
function getUrlVars() {
var map = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
 map[key] = value;
});
return map;
}    
function createRequestObject() {
            var ro;
            // Mozilla, Safari,...
            if (window.XMLHttpRequest) {
             ro = new XMLHttpRequest();
             if (ro.overrideMimeType) {
              ro.overrideMimeType('text/xml');
              // See note below about this line
             }
             // IE
            } else if (window.ActiveXObject) {
             try {
              ro = new ActiveXObject("Msxml2.XMLHTTP");
             } catch (e) {
              try {
               ro = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (e) {}
             }
            }
            if (!ro) {
             alert('Giving up :( Cannot create an XMLHTTP instance');
             return false;
            }
            return ro;
        }
        function sndReq(param,server,handler) {
            //location.href = server+"?"+action; //uncomment if you need for debugging
            http = createRequestObject();
            http.open('GET', server, true);
            http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            http.onreadystatechange = handler;
            http.send(param);
        }
        handler_function = function()
        {
            if(http.readyState == 4)
            {
             if (http.status == 200)
             {
              document.getElementById("your_div_element").innerHTML = http.responseText;
             }
             else
             {
              alert('There was a problem with the request.');
             }
            }
        }

</script>
</head>
<body>
<div id="your_div_element"></div>
<script>
var getvars= getUrlVars();
sndReq(null, getvars['action'], handler_function);</script>
</body>
</html>
Martin Hoegh
how to request content via url link
Power-Mosfet
Use the sndReq(null,"b.html", handler_function); where b.html is the url like http://yourserver.com/b.html. Remember you can't request content from an other domain like http://theirserver.com/b.html.
Martin Hoegh
could you please just the url link which load the into <div id="your_div_element"></div>
Power-Mosfet
Sorry, I do not get the question?
Martin Hoegh
Sorry, my bad.I just want to know how load the content from (1.html) into default page (main page on server), using the above code.somthing likewww.myweb.com/index.html?action=file1.html
Power-Mosfet
You need to request the http GET variable (action=file1.html). Take a look here: http://snipplr.com/view/19838/get-url-parameters/ on how to do that in JavaScript
Martin Hoegh
How ?how do i get variable in Martin Hoegh code. and load content in to div location.
Power-Mosfet
I've now updated my answer and put in the getUrlVar() function. So now you can call martins_script.html?action=file1.html. I've not tested the script and it may be buggy.
Martin Hoegh
I have test it, not working
Power-Mosfet
You have to debug the code:)
Martin Hoegh
Sorry Im an total nowbie, i do this just for hobby with a notepad. i was hoping to find a working sample. thank you anyway.
Power-Mosfet
Its ok. I've now tested the script and updated. Is works now. Put the script in a file, call it something like "test.html". Write a new html file, called it eg "get.html" and write some text in it. Put both files in your web root. Call test.html?action=get.html and bingo!
Martin Hoegh
Just corrected one more thing and made a live example: http://wms.mapuse.net/stackoverflow/test.html?action=get.html
Martin Hoegh
You just made my day, thank you for your time.
Power-Mosfet
+1  A: 

html:

//Page A
<html>
<head><title>Page A</title></head>
<body>
<div id="pageB"></div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
    $(document).ready(function () {
        $('#pageB').load('pageb.html')
    });
</script>
</body>
</html>
fredrik