Can you embed a 3rd party javascript within your own?

AdHustler

New member
Aug 24, 2007
4,377
44
0
This is what i'd like to do. I have a website where I want to place 1 javacript code (hosted on my server) on their checkout page.

I'd like to embed within that 1 code, several tracking codes.

Does anyon e know how I can accomplish this?
 


You need to upload the javascript file in webpage and call it

<script language="JavaScript"
src="http://yoursite.com/script/thescript.js"
type="text/javascript"></script>

And use a similar code inside the thescript.js to call the third Script..

Something like that. I also haven't been coding much lately :)

PS: PM me the details. I will have my developer look at it if you don't figure it by tonight
 
thanks man ill try to figure it out - if i cant ill hit you up - if anyone else has examples or links to tutorials id appreciate it
 
How to Import Javascript into another Javascript file

There is a jQuery function to do this and a Prototype function to do this. You can also make your own.

I don't have any of the code around right now on this computer from when I last did it, or I'd post it. You should be able to find some though from the link.

It's just easier though to reference the 3rd party script by URL in the src element on the HTML page though.
 
not clear on what you're needing to be done -- if it's:

on the merchant's page, you want to execute JS from your server. within your JS, you want to load tracking scripts that are hosted on other servers.

this is usually a bad idea, and probably won't be allowed to execute.

there are newish ways to get it done securely:
Secure Integration of 3rd-Party Javascript Code

Microsoft WebSandbox Technology Preview
OpenAjax Hub 2.0 Specification - MemberWiki
google-caja - securing Javascript-based web content
Ajaxian Using YQL as a proxy for cross-domain Ajax
easyXDM - Cross-domain messaging made easy

but you'd probably be better off using iframes or server-side scripts to track whatever, if possible:
Safe implementation of script tag hack to do XSS? - Stack Overflow
Invoke JavaScript on 3rd party domain - Stack Overflow

if you're able to host all the JS on the same server, you can do as rexibit suggested:
Include javascript file inside javascript file? - Stack Overflow
 
It's not fully clear but if you want dynamically load a JS from another JS, this is a viable solution.

This is the function to embed:
Code:
function embedJavascript(jsFile) {
    var myHead = document.getElementsByTagName('head')[0];
    var myScript = document.createElement('script');
    myScript.setAttribute('type', 'text/javascript');
    myScript.setAttribute('src', jsFile);
    myHead.appendChild(myScript);
}
This is the function call:
Code:
embedJavascript('/script/javascriptURI.js');
 
Creating a Multiple Script File

You might have a collection of scripts that you call from time to time, like a scripts which calculate time zones or distance, or maybe scripts that create some effect or accent on your page. For recurring Javascripts, consider grouping them together into one file.
For this example, name the group Javascripts file scriptfile.js (choose whatever you want) and say it contains the updatepage, emailpage, and caltimezone scripts. As you copy each Javascript into the file, make sure it has a unique function name such as with this condensed version:
function updatepage() {var m="Page updated "+document.lastMo.......} function emailpage() {mail_str = "mailto:?subject=....} function caltimezone() {var timerID ; function tzone(tz, os, ds, cl) {this.ct =......} Place the script file of all the Javascripts in the head of the header.php template file between the meta tags and the style sheet link. It will just sit there, loaded into the browser's memory, waiting for one of the scripts inside to be called.
<script type="text/javascript" src="/scripts/scriptfile.js"></script> In the spot in your post where you would like to use the Javascript, call it as follows:
<script type="text/javascript"> <!-- updatepage(); //--></script>