/    Sign up×
Community /Pin to ProfileBookmark

Hello everyone, I’m experimenting a bit with ajax, and I was wondering why I can’t execute a script (javascript!) that’s placed in a file that’s fetched from the server…

E.g: Here’s my page that fetches other pages to change the content:

[url]http://elife.ramakersonline.be/email/[/url]

and here’s one of the pages that’s being fetched :

[url]http://elife.ramakersonline.be/email/small/home.html[/url]

In home.html, I placed the following code:

<script type=”text/javascript”>
document.write(“test”);
</script>

This code words perfectly in the page itself, but when it’s fetched, it doesn’t.

Here’s some extra info:

[LIST]

  • [*]

    I use relative url’s, but it doesn’t work with absolute url’s either.


  • [*]

    If I just type “test” in home.html, it does actually work, but I want to be able to use javascripts so…


  • [/LIST]

    to post a comment
    JavaScript

    5 Comments(s)

    Copy linkTweet thisAlerts:
    @rootMar 27.2007 — you need to remove the tags.

    when you have fetched the file, you need to pass the string to eval();

    if your script returns a control value, you would use

    var result = eval(theResponseText);

    otherwise a straight

    eval(theResponseText);

    will do, theResponseText is the variable that would be replaced by your returned string from the AJAX request. having a conrol value returnd is a better method as you can then test the value of result to determin if the eval was a success.
    Copy linkTweet thisAlerts:
    @TombombadilauthorMar 27.2007 — It works, but I can't use it because (1) it removes all my other js functions (the nav is inserted with js as well) and (2) it wouldn't allow me to add html to the page that's being fetched
    Copy linkTweet thisAlerts:
    @rootMar 27.2007 — if your js file is external, you dont need tags.

    you will lose all your existing functions if you rewrite the page, its starting from a blank, so yeah, it wont work...

    You can add to an existing page or modify it but you cant completely rewrite it and especiially with a document.write !, think about it... document... write....
    Copy linkTweet thisAlerts:
    @TombombadilauthorMar 27.2007 — I think I solved the problem. I added <script type="text/javascript" id="script"></script> to the index page.

    Every time a file is fetched, the src of the <script id="script"> is changed to add an external javascript file that contains the extra javascript needed.

    (it only works in FF atm because I'm having problems with IE not loading the javascript functions)
    Copy linkTweet thisAlerts:
    @rootMar 27.2007 — It is so simple yet you are making things more complicated.

    You only need the RAW script, NO TAGS <-- hint, pass that RAW SCRIPT to eval().

    Thats it, all you need to do, it wont erase or delete other loaded functions but add.

    If you ever do a document.write AFTERR the document has loaded, you will instantly rewrite the page with that new data and delete the old.

    If you document.write before the document has loaded, anything you write at time of code execution will be included in the current document.

    Is that clear enough?
    ×

    Success!

    Help @Tombombadil spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.2,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...