好的,我得到了另一个问题 我有一个js文件,我需要包含在我的页面上(我没有权限编辑这个js文件)
在javascript里面有一个函数,它有一行我需要在那里编辑一个变量。
让我们假设:
代码:
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'
我需要在sid =之后添加一些东西 所以我成了例如: 代码:
&sid=AlssfIT_e6a00014f247fe39de1b_1
我补充说:AlssfIT_
任何想法如何实现这一目标? 我尝试过类似的东西 代码:
str.replace("&sid=","&sid="+kwd);
在我“包含”js文件后,但显然无法正常工作
答案 0 :(得分:2)
我认为你的做法是错误的。如果notice是全局空间中的变量,您可以正常替换它。
window.someObject.notice = window.someObject.notice.replace("&sid=","&sid="+kwd);
这当然只有在notice是一个可以在全局命名空间中导航并且不在闭包内的变量时才有效。如果var
function() {...}
声明,则它位于闭包内
但是,假设存在对该变量的全局访问权限,那将是实现此目标的最简单方法。
如果没有,您可以尝试抓取脚本的内容并执行它,希望覆盖原始代码。 这只有在您提取的脚本和脚本来自同一个来源(域,子域,端口,协议,其他一些东西)时才有效 - 否则由于_ 同源策略而无法实现 _ 强>
假设你是同一个起源,你可以做这样的事情(为了简单起见使用jquery)
( 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);
});
} )()
这样的事情可行。
答案 1 :(得分:0)
尝试正则表达式:(未经测试)
myregexp = new RegExp("/&sid=/", "gims");
str.replace(myregexp, "&sid=" + kwd);