SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Javascript refresh, and variable reload

    In October last year I wrote a small script that refreshes javasacript values.
    The original thread can be found here..........
    http://www.sitepoint.com/forums/showthread.php?t=135179

    the main part of the function is this
    Code:
    function changeScript(newJScript) {
    oldScript=document.getElementsByTagName("script")[document.getElementsByTagName("script").length-1];
    var newEl = document.createElement('script');
    newEl.setAttribute('type', 'text/javascript');
    newEl.setAttribute('src', newJScript);
    oldScript.parentNode.replaceChild(newEl,oldScript);
    changeScript.el = newEl;
    return true;
    }
    I used to use a javascript based database at the time, and I used the script above to import the new javascript values.

    ie.
    variable_set1.js would look like this.
    Code:
    newArray="a,b,c,d,e"
    and variable_set2.js would be something like this
    Code:
    newArray="f,g,h,i,j,k"
    I would then call the new js script, and using setTimeout check the variable newArray had changed, and when it had, use the new variables.

    Since learning server side programming, I have found this script to be even more beneficial.

    I learnt that the output can be sent directly from a server side script to the required field.

    Here's a basic ASP example
    Code:
    <%
    '----- no caching ------
    Response.Expires = 0
    Response.Expiresabsolute = Now() - 2
    Response.AddHeader "pragma","no-cache"
    Response.AddHeader "cache-control","private"
    Response.CacheControl = "no-cache"
    Response.ContentType="application/x-javascript"
     
    '----- get new variable ( this is from a text file, but could be database )-----
     
    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    varFile=Server.MapPath("variable.txt")
    Set varFileOpen=fso.OpenTextFile(varFile, 1)
    newVar=varFileOpen.readall
    varFileOpen.Close
    set varFileOpen = nothing
     
    '---- this is the interesting part -------
    '---- sends newVar directly to webpage -----
     
    %>document.getElementById("var_holder").firstChild.NodeValue=<%=newVar%>;
    And that's it. A very handy little script.

    I hope you all enjoy experimenting, and get alot of use from it.
    Last edited by Markdidj; Oct 12, 2004 at 06:34.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Any browser issues one should be aware of before using this script (other than the obvious Netscape/IE4)?

  3. #3
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't know Vinnie. Last year it didn't work in Opera. I left it in their wish-list forum, and this year it works. Don't know if it was because of my suggestion.

    It's been tested, and works, in IE6, Firefox, Netscape 7+, and Opera. Nice Eh?

    Would be nice if someone could try it in other browsers, ie Safari

    My drummer uses it to import new rhythm arrays, so it can be tested there. I use it on my chatroom as well, but that's a members only function.
    Last edited by Markdidj; Oct 12, 2004 at 06:02.
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    It looks like a pretty cool script. Unfortunately I can't test in Safari or other Mac browsers for you, but if I ever find a use for it I'll send you a link of it in action . (I've been working on a lot of boring XML/JSP stuff lately, no cool client-side stuff unfortunately)

  5. #5
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    This script allows sessions to be set in javascript. Kind of.........

    using changeScript("newscript.asp?var="+new_var)
    then in newscript.asp getting the querystring("var") and set it as a session. Cool......
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  6. #6
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What would be the best way to transfer long sentances to the new script. In my chatroom I used cookies......
    Code:
    <form method="POST" action="javascript:sendData()">
    <input type="text" id="messSent">
    <input type="submit">
    </form>
    <script type="text/javascript">
    function sendData() {
    sentance=document.getElementById("messSent").value
    sentance=sentance.replace(/\s/g,"_")
    document.cookie = "newLine="+sentance;
    changeScript("cookieChatSave.asp");}
    </script>
    with cookieChatSave.asp reading the cookie and saving it to a text file.

    Would it be better to send it through the querystring, or doesn't it matter?
    eg. changeScript("cookieChatSave.asp?newLine="+sentance)

    Thanks

    And one more thing, what security measures should I take in doing it either way....
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if


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
  •