Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    New to the CF scene
    Join Date
    Dec 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    adding javascript dynamically

    Hi,

    I'm currently looking at data-piping ie making server calls without reloading the page, the server returning some data to a cookie and the calling page using that same data on the page using DHTML. So far so good...

    But is it possible to actually use javascript to actually write more javascript onto the page? Or, alternatively, is it possible to dynamically add a call to an external javascript file.

    Something like...

    document.writeln('<script src="script.js")

    The idea being that the server could return functions for the calling page to use, which would be very nice for complex web applications.

    If you can do this, what issues do you need to be aware of?

    If you can't, are there any ways of achieveing the same things? I'm thinking of things like dynamically adding a scriptlet call to the page in Windows IE, though a more cross-browser solution would be cool.

    Any help would be appreciated,

    Cheers,

    Alan Hill

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    DOM compliant solution:
    Code:
    script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'serversidescript.asp';
    document.getElementsByTagName('head')[0].addChild(script);
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #3
    Regular Coder
    Join Date
    May 2002
    Location
    Helsinki, Finland
    Posts
    231
    Thanks
    0
    Thanked 1 Time in 1 Post
    Originally posted by Vladdy
    DOM compliant solution:
    Code:
    script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'serversidescript.asp';
    document.getElementsByTagName('head')[0].addChild(script);
    Replace with method appendChild().
    Zvona
    First Aid for
    Web Design

  • #4
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72 W. 48' 57" , 41 N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    if that doesn't work, what you might try, is using an iframe. then, you could document.write() into the iframe, and have the iframe load the javascript.

    eg

    <script>
    self.IFRAME.document.write('<script src="serversidescript.asp"></"+"script>');
    self.IFRAME.someJavaScriptFunction();
    </script>
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for correction, Zvona. It was too late to think straight when I was typing it.
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #6
    New to the CF scene
    Join Date
    Dec 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks to everyone who replied, I'm very grateful.

    Anyone here actually used this technique? It would be cool if someone could show me a site that uses this technique. Alternatively, if anyone knows a guide to dynamically loading javascript, and how the browsers deal with it, that would be cool too.

    Cheers,

    Alan Hill

  • #7
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Yes - there's a working demo of this on the Javascript FAQ section of the Javascript City Forums ... the forums are down at the moment, so I can't give you the full URL.

  • #8
    New Coder
    Join Date
    Dec 2002
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi

    The forums you mention seem to be up now but I cannot find the page you talk about. Vladdy I cannot get your code (with the fix) to work at all. It doesn't cause an error and it does get to the alert statement after the final line of code, however nothing in the file i'm appending is running. At the moment all thats in there is one alert statement. You posted to another message of mine (practically the same question) and the code was slightly different but that doesn't seem to work either.

    - Brett

  • #9
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No Problem.

    The FAQ is on this thread

    The demo is on this link

    If I can help further, just let me know.

  • #10
    New Coder
    Join Date
    Dec 2002
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi

    OK I got the script working and its great, although it doesn't offer any advantage over just document.write("<script>") for me and in fact I think that is NS4 compatible so in that sense it's definately a winner. Anyone know any other method thats NS4+ compatible as well?

    - Brett

  • #11
    New Coder
    Join Date
    Sep 2002
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi MrBrett,

    There's no problem sending data to the server - you can do this by var i = new Image(); i.src="myscript.php?"+dataofyourchoice

    The problem is trying to get data back from the server without having to refresh your page.

    I've not seen a alternative workable solution - without using Java applets or flash.

  • #12
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The example I posted does just that.
    You can send data to the server using the query string:
    script.src = 'serversidescript.asp?Field1=SomeData';
    and return data from the server in a form of javascript:

    returnedData=new Array(
    '<%=getData(1)%>',
    '<%=getData(2)%>',
    '<%=getData(3)%>',
    '<%=getData(4)%>',
    '<%=getData(5)%>'
    )

    updatePage();

    where updatePage function resides on a page and updates page elements using returnedData array
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #13
    New to the CF scene
    Join Date
    Dec 2002
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    you write the info to a cookie, and the javascript reads from the cookie.

    That's how it' s done!

  • #14
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    How about - use Vladdy's DOM method to call a PHP or ASP script (including query string data) - that script itself generates javascript data (ergo plain text) and prints it out, which is then loaded back into the browser when you appendChild.

  • #15
    New Coder
    Join Date
    Dec 2002
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi

    Nah I actually don't need the server bit (or at least server side work), I just thought my need fitted under this topic because I need the load javascript dynamically part. Anyway, so it looks like (to be all (roughly) 4+ browser compatible I need to use the document.write method instead of this ... *sigh* this method is so much nicer).

    Thanx for the help anyway

    - Brett


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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