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

JS刷新script中的src可否动态获取数据??
JS刷新script中的src可否动态获取数据??

比如页面上这样写:
<script src="http://hq.sinajs.cn/list=sz000009" type="text/javascript" charset="gb2312"></script>


var randtime = (new Date()).getTime();
var url = 'http://hq.sinajs.cn/rn=' + randtime + '&list=sz000009';
gpjs.src = url;
var elements=hq_str_sz000009.split(",");


可否用上面动态获取数据,感觉似乎行不通呀,这样似乎是异步的方式,但后面的执行感觉似乎行不通。望高手给个方案。
谢谢。

------解决方案--------------------
动态加载?

加载下
HTML code

<!DOCTYPE HTML>
<html lang="">
<head>
    <meta charset="gbk">
    <title></title>
</head>
<body>
    <script type="text/javascript">
;(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';
                links[i].href = urls[i];
                dc.getElementsByTagName('head')[0].appendChild(links[i]);    
            }
            callback();
        }
        return {
            load: function(option, callback){
                var _type = option.type || 'js',
                    _url = option.url,
                    _callback = callback || function(){};
                switch( _type ){
                    case 'js':
                    case 'javascript':
                        createScript(_url, _callback);
                        break;
                    case 'css':
                        createLink(_url, _callback);
                        break;
                }
                return this;
            }
        }
    }();
    w.Cme ? '' : w.Cme = {};
    w.Cme.loader = loader;    
})(window);
            Cme.loader.load({
                url: [
                    'http://hq.sinajs.cn/list=sz000009'
                ]
            },function(){
                alert( hq_str_sz000009 );
            })  

    </script>
</body>
</html>