简体   繁体   中英

How to Cause Page to Redirect to a javascript snippet?

This example shows how to redirect to a url:

<meta http-equiv="refresh" content="0; url=http://example.com/" />

But, i do not want to redirect to a url, i want to redirect to a javascript snippet:

javascript:(function(){s=document.createElement("script");s.type="text/javascript";s.src="https://www.diigo.com/javascripts/webtoolbar/diigolet_b_h_b.js";document.body.appendChild(s);})();

Goal is to do this with a single file.

I tried replacing the url in the meta redirect above with my js snippet, but it fails. I believe the problem is the double-quotes embedded in the js:

<meta http-equiv="refresh" content="0; url=javascript:(function(){s=document.createElement("script");s.type="text/javascript";s.src="https://www.diigo.com/javascripts/webtoolbar/diigolet_b_h_b.js";document.body.appendChild(s);})();" />

I tried replacing the double-quotes in the js with &quot; but that did not work. Also tried %22, also did not work.

I'm not trying to redirect using javascript, i'm trying to redirect to javascript (using meta or another non-js method). However, if there's a way to execute the above script using another piece of script, that's fine.

Not trying to redirect to a .js file -- i want to embed the js snippet inside the redirect.

This question is not a duplicate, because they are not redirecting a page, they are redirecting a link.

i guess you want to load the js file Dynamically! if so ..,here is my code i used before

(function( window, undefined ){
//设置meta 禁止缓存
document.write('<meta http-equiv="content-type" content="text/html; charset=GBK"><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache">');

//加载公共JS文件
var jsFile = [
    CONTEXT_PATH_NAME+"/js/jquery-1.4.2.min.js",
    CONTEXT_PATH_NAME+"/js/jquery.easyui.min.js",
    CONTEXT_PATH_NAME+"/js/jquery.cookie.js",
    CONTEXT_PATH_NAME+"/js/Toolbar.js",
    CONTEXT_PATH_NAME+"/js/common.js",
    CONTEXT_PATH_NAME+"/js/ajax-request.js",
    CONTEXT_PATH_NAME+"/js/validator.js",
    CONTEXT_PATH_NAME+"/js/tooltip_split.js",
    CONTEXT_PATH_NAME+"/js/cattMsg.js",
    CONTEXT_PATH_NAME+"/js/watermark.js",
    CONTEXT_PATH_NAME+"/html/js/session.jsp"
];
var jsTags = "";
for(var i in jsFile) {
    jsTags += '<script type="text/javascript" src="' + jsFile[i] + '"></script>';
}
document.write(jsTags);
})( window );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM