homepage Welcome to WebmasterWorld Guest from 54.211.138.180
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Website
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Including external JS within ajax
seks




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

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:

switch(getCheckedValue(document.forms['myForm'].elements['type']))
{
case 'frame':
//tab = '<iframe src="'+ajaxRequest.responseText+'" width=100% height=7000></iframe>';
tab = '<script src="'+ajaxRequest.responseText+'"></script>';
break;

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!

 

httpwebwitch




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

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?

try:

someElementOnYourPage.innerHTML = ajaxRequest.responseText

seks




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

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.

httpwebwitch




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

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.

seks




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

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

stuartc1




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved