日期:2014-05-16  浏览次数:20739 次

js文件问题
<script type="text/javascript" src="http://int.dpool.sina.com.cn/iplookup/iplookup.php? format=js"></script>
<script type="text/javascript">var province = remote_ip_info.province;if(province != '北京' && province != '广东' && province != '上海' && province != '成都' && province != '深圳' ) {window.location = "http://www.baidu.com";}</script>

这段代码是根据不同城市ip 调整到指定页面,

如何把这段js 放在一个js文件里,,

我建立一个js文件
document.write("<script type="text\/javascript" src="http:\/\/int.dpool.sina.com.cn\/iplookup\/iplookup.php? format=js"><\/script>");


var province = remote_ip_info.province;
if(province != '北京' && province != '广东' && province != '上海' && province != '成都' && province != '深圳' ) 
{
window.location = "http://www.baidu.com";
}
  
这样写 没效果,


------解决方案--------------------
页面调用该文件了?
------解决方案--------------------
楼主 
document.write('<script type="text\/javascript" src="http:\/\/int.dpool.sina.com.cn\/iplookup\/iplookup.php? format=js"><\/script>');

这样写,没有阻塞页面的加载,只是发送了一个请求,接着马上就用到了
province = remote_ip_info.province,
但是这时 外链的js内容还没加载完毕,所以就没效果了。

可以 把wirte的内容
换成普通阻塞加载
<script src="">。。。</script>

或者
参考 1楼
http://topic.csdn.net/u/20120530/19/d8c76d3c-f825-4c41-987c-bcd8da7e2be9.html
------解决方案--------------------
http://www.chhblog.com/Web/ArticleView.aspx?ArticleID=192
参考
------解决方案--------------------
HTML code

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>        
    </head>
    <body>



        <script>
            ;(function(w){
                var loader = function(){
                    var dc = document;
                    function createScript(url, callback){
                        var urls = url,
                            scripts = [],
                            completed = 0;
                        for( var i = 0, len = urls.length; i < len; i++ ){
                            scripts[i] = dc.createElement('script');
                            scripts[i].src = urls[i];
                            dc.getElementsByTagName('head')[0].appendChild(scripts[i]);
                            if( scripts[i].readyState ){ //ie
                                scripts[i].onreadystatechange = function(){
                                    if( this.readyState == 'complete' || this.readyState == 'loaded' ){
                                        this.onreadystatechange = null; //确保事件不被处理2次
                                        completed++;
                                        completed >= urls.length ? callback() : '';
                                    }
                                }
                            }else{ //not ie
                                scripts[i].onload = function(){
                                    completed++;
                                    completed >= urls.length ? callback() : '';
                                }                            
                            }
                        }
                    }
                    function createLink(url, callback){
                        var urls = url,
                            links = [];
                        for( var i = 0, len = urls.length; i < len; i++ ){
                            links[i] = dc.createElement('link');
                            links[i].rel = 'stylesheet';