SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    Non-Member
    Join Date
    Dec 2004
    Location
    London
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple javascript question

    I am a real javascript noob, and I can't find this answer at all. If I have 1.html and 2.html, how do I, with javascript, include 2.html in 1.html?

    thanks

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I don't think it's not possible to do that using Javascript. You'll probably have to use an iframe.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    Non-Member
    Join Date
    Dec 2004
    Location
    London
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lilleman
    Hi,

    I don't think it's not possible to do that using Javascript. You'll probably have to use an iframe.
    It's not even possible to write the contents of the other html file?

  4. #4
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I now realized that it might be possible to accomplish by using XMLHttpRequest. I've never used it myself though, so I can't help you with it, but I'm sure there are others here with experience of using it.
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  5. #5
    Non-Member
    Join Date
    Dec 2004
    Location
    London
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lilleman
    Hi,

    I now realized that it might be possible to accomplish by using XMLHttpRequest. I've never used it myself though, so I can't help you with it, but I'm sure there are others here with experience of using it.
    Oh man! This is harder than I thought! I thought it would be so much easier to get something from an outside source and print it on the page (it could be a txt file even). Thanks for that Erik.

  6. #6
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Quote Originally Posted by redbaron
    Oh man! This is harder than I thought! I thought it would be so much easier to get something from an outside source and print it on the page (it could be a txt file even). Thanks for that Erik.
    I think you should use php, asp or jsp to do it. Because javascript is only used on client side, not server side.

  7. #7
    Non-Member
    Join Date
    Dec 2004
    Location
    London
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by okhand
    I think you should use php, asp or jsp to do it. Because javascript is only used on client side, not server side.
    It has to be javascript, the reason being is the file must be .htm or .html.

  8. #8
    Non-Member
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    4,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello

    you have to tell a bit more what you want to do
    a full page, just a part of a page, a menu ?

    Seems a returning question:

    http://www.sitepoint.com/forums/showthread.php?t=243776

    http://www.sitepoint.com/forums/showthread.php?t=238510

  9. #9
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I have had a similar dilemna.

    In 1.html put this:
    Code:
    <script src="2.js"></script>
    In 2.js, put whatever you want.

  10. #10
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok XMLHTTP you can pull in any information you want, I use it for templates, although I use it in asp not javascript.

    Code:
    <script type="text/javascript">
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions.
    // and security blocked creation of the objects.
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
      xmlhttp = new XMLHttpRequest();
    }
    
    /* real work starts here... */
    
    function check (address) {
      if (xmlhttp) {
        xmlhttp.open("GET", address,false);
        xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) {
    	document.write('<BASE HREF="' + address + '">');
          document.write(xmlhttp.responseText)
        }
      }
     xmlhttp.send(null);
      }
    }
    </script>
    to use it you will need to call it using
    <a href="javascript:check('http://www.eycdwebdesign.co.uk');"> Click </a>

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function check (address)
    {
        document.write(...) 
    
    }
    Won't that send 1.html into the ether?

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geniusgoalie
    Okay, I have had a similar dilemna.

    In 1.html put this:
    Code:
    <script src="2.js"></script>
    In 2.js, put whatever you want.
    Besides not being very clear on what you mean, it doesn't work for me in IE6 or FF1.0. The js parser doesn't seem to be able to handle html between <script> tags--which seems appropriate--and that is what happens to the src file; it is inserted between the <script> tags.

  13. #13
    Non-Member bronze trophy geniusgoalie's Avatar
    Join Date
    Sep 2004
    Location
    Buffalo, USA
    Posts
    979
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 7stud
    Besides not being very clear on what you mean, it doesn't work for me in IE6 or FF1.0. The js parser doesn't seem to be able to handle html between <script> tags--which seems appropriate--and that is what happens to the src file; it is inserted between the <script> tags.
    Oh, sorry for not being clear. What I meant was, if it is an advertisement code, you must enter the javascript (not HTML). If it is html that you want displayed, an iframe would be easiest.

  14. #14
    &#083;itePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44░ 56.537' W 123░ 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the included markup comes from an HTML page, a frame or iframe is the logical choice. If it is just modifications to the existing page, a .js file can store the values to be added/changed and innerHTML or node methods can be used to generate the changes. You could even selectively add a .js file after the fact, with node methods, if desired.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •