Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Including external JS within ajax



10:08 pm on Apr 18, 2008 (gmt 0)

5+ Year Member

Hi, I have a multitabbed interface that interacts with ebay. Ebay's new API presents me with a URL which contains JS which contains a list of items etc.

Now this would be fine if I was feeding the script with a POST form or such, but I'm using AJAX to update the tabs.

How do I dynamically update my page with the JS at the url?

I tried:

case 'frame':
//tab = '<iframe src="'+ajaxRequest.responseText+'" width=100% height=7000></iframe>';
tab = '<script src="'+ajaxRequest.responseText+'"></script>';

Where tab is the contents of the tab, but the script just ends because I don't know how to escape </script> (I tried '<'+'script/>' and things to no avail.)

Keep in mind the iframe I am including this in doesn't have <script> tags around it.

Thanks a lot guys!


1:49 pm on Apr 19, 2008 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

this is a confusing question

if you load AJAX that contains a <script>, then inject that HTML into your DOM, the script you received will usually execute

does ebay's API URL "contain" JS, or is it actually a script, with mimetype text/javascript and the whole enchalada?


someElementOnYourPage.innerHTML = ajaxRequest.responseText


8:13 pm on Apr 19, 2008 (gmt 0)

5+ Year Member

It's actually a complete script (about 5+ pages of JS). Yeah that's what I thought it should do, too.

The way the tabs are created is through a built in function createNewTab() which takes tab as an argument. My declaration of tab behaves correctly if i document.write() it, but then it opens in a new window when I want it to be in a frame. Perhaps the tab scripts I'm using don't allow js inside the tabs.

If I do it through the iframe method (which is commented out on my code posted above), I just see the source code -- it's not being parsed. I think the easiest thing will be for me to throw together a perl script which adds <script> tags to that then I can just put it in an iframe.


11:17 pm on Apr 19, 2008 (gmt 0)

WebmasterWorld Administrator httpwebwitch is a WebmasterWorld Top Contributor of All Time 10+ Year Member

i think you're right.

Lately I've been using Mootools JS library, which comes with a "Asset.javascript [docs.mootools.net]" class - essentially you can load up an external javascript and execute it with one line of code. Worth a look; might be ideal for what you're doing.


7:14 am on Apr 20, 2008 (gmt 0)

5+ Year Member

Thanks a lot! I'm taking mootools for a spin and even found a tabs plugin for it.. It seems smooth and lovely :)


10:50 am on Apr 22, 2008 (gmt 0)

10+ Year Member

Just a thought.... have you tried using eval() for the returned js? I think this is the only way to have the js run when loaded this way. I may be wrong and would be interested in find other ways to do this.

Featured Threads

Hot Threads This Week

Hot Threads This Month