2

Ok so another issue i got I have a js file that i need to include on my page (i dont have access to edit this js file)

Inside that javascript there is a function wich has a line that i need to edit a variable in there.

lets assume:

Code:

var links     = [{"offer_id":"1","title":"Text!","url":"http:\/\/www.site.com\/tracker\/1\/?http:\/\/site.com\/click.php?aff=9917&camp=5626&crt=13346&sid=e6a00014f247fe39de1b_1","footer_text":"text","blank_referrer":"1","active":"1","value":"0.40","creation_time":"1327785202"}];


notice :     '&sid=e6a00014f247fe39de1b_1' 

i need to add something right after sid= so that i becomes for example: Code:

&sid=AlssfIT_e6a00014f247fe39de1b_1 

i added: AlssfIT_

any ideas how to achieve this ? i tried something like Code:

str.replace("&sid=","&sid="+kwd); 

right after i "include" the js file but aparently is not working

1
  • Yes, that would not work. You do not "include" a js file when you have a script tag. What you are doing is that you're telling the browser the code in that file will be needed for the page to run. The browser will fetch that file at its own pace. The only guarantee is that all the scripts on your page will execute in the order that they appear. Commented Jan 29, 2012 at 0:02

2 Answers 2

2

I think you're going about it the wrong way. If notice is a variable in the global space you can just replace it normally.

window.someObject.notice = window.someObject.notice.replace("&sid=","&sid="+kwd); 

This will of course only work if notice is a variable that is navigable to in the global namespace and is not inside a closure. It is inside a closure if it has a var declaration inside a function() {...}

But, assuming that there is global access to that variable, that will be your easiest way to achieve this.

If not, you can try grabbing the contents of the script and executing it hopefully overwriting the original code. This will only work if your script and the script you are fetching are from the same origin (domain, subdomain, port, protocol, a few other things) - it is impossible otherwise due to the _Same Origin Policy_

Assuming you are at the same origin, you could do something like this (using jquery for simplicity)

( function() {
// First we need the url of the script, we can grab it out of the element directly or it can be hard coded
var scriptLocation = $('script#the-id-of-the-script-element').prop('src');

// Now that we have the location fetch the script again so we can get it as plaintext
// this will usually not do another HTTP request since your browser has it cached
$.get(scriptLocation).done(function(text) {  // I prefer the deferred syntax as being more explicit, this is equivalent to $.get(scriptLocation, function(text) {
  var newText = text.replace("&sid=","&sid="+kwd); 
  eval(newText);
});
} )()

Something like this could work.

Sign up to request clarification or add additional context in comments.

10 Comments

Same Origin Policy or Cross-Origin Resource Sharing (CORS) - it works in all normal browsers and IE8
i`m on the same server, just different locations. this is for example my js file location site.com/tracker/locker.js?guid=74f2464918d5e936
not sure how to set var scriptLocation = $('script#the-id-of-the-script-element').prop('src'); correctly
@misulicus - if you know the location of the script (that link does not work) just hard-code it. That is the easiest thing to do.
Ok so i got it like this now: <script type="text/javascript" language="javascript"> var kwd = "{keyword}_"; var virpath = "{virtual_path}"; ( function() { var scriptLocation = virpath+'/tracker/locker.js?guid=74f2464918d5e936'; $.get(scriptLocation).done(function(text) { var newText = text.replace("&sid=","&sid="+kwd); eval(newText); }); } )() </script> firebug shows me an error: $.get(scriptLocation).done is not a function
|
0

try regex: (not tested)

myregexp = new RegExp("/&sid=/", "gims");
str.replace(myregexp, "&sid=" + kwd);

3 Comments

nope not working. i added it after i load the .js file in my main file
are you trying to include the js file from the other server to your server?
JS does not support s flag for regexps, m is not cross-browser, and, most importantly, - regexp is not needed at all here, 'coz in this case you can simply write str.replace( "&sid=", "&sid=" + kwd );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.