开发者

Reload a .js file without pressing F5

i load a page with jquery .load(file.php)

i have a .js include in the file.php like: <script src='js/script.js' type="text/javascript" language="javascript"></script>

When i load the file.php, he wouldn't load my JS file... does anybody know why and how t开发者_如何学Co solve it?

Thanks


I dont know why, but jQuery clear script elements when load html.

I`ve faced the same problem recently and after days searching in google I could implement a workaround for it.

 $.ajax({
     url: 'mypage.aspx',
     data: eval('(' + MyData + ')'),
     dataType: 'html',
     success: function (data) {
         var $div = $('<div></div>').hide().appendTo($("#MY_DIV")).html(data).remove();
         var innerData = $(data).html();
         $("#MY_DIV").html(innerData);
     }
 });

Other possible way is add the scripts in the current page:

 $.ajax({
     url: 'mypage.aspx',
     data: eval('(' + MyData + ')'),
     dataType: 'html',
     success: function (data) {

         //Add to DOM scripts from the loaded page.
         var headID = document.getElementsByTagName("head")[0];
         var arr = data.match(/<script(.|\n|\t)*?script>/gi);
         if (arr != null) {
             for (var i = 0; i < arr.length; i++) {
                 if (arr[i].match(/<.*?src=.*?>/) != null) {
                     var sSrc = arr[i].match(/src=".*?"/)[0];
                     sSrc = sSrc.replace('src=', '').replace('"', '').replace('"', '');
                     var newScript = document.createElement('script');
                     newScript.type = 'text/javascript';
                     newScript.src = sSrc;
                     headID.appendChild(newScript);
                 }
             }
         }
         var arr = data.match(/<link(.|\n|\t)*?>/gi);
         if (arr != null) {
             for (var i = 0; i < arr.length; i++) {

                 if (arr[i].match(/<.*?href=.*?>/) != null) {
                     var sHref = arr[i].match(/href=".*?"/)[0];
                     sHref = sHref.replace('href=', '').replace('"', '').replace('"', '');
                     var newLink = document.createElement('link');
                     newLink.rel = "stylesheet";
                     newLink.type = "text/css";
                     newLink.media = "all";
                     newLink.href = sHref;
                     headID.appendChild(newLink);
                 }
             }
         }

         $("#MY_DIV").html(data);
     }
 });


Check the paths, make absolute path for .JS file, it should load.

<script src='/js/script.js' type="text/javascript" language="javascript"></script>

Just to be positive try it like this...

function ajax(url, selector){   
    $.ajax({
    type: "GET",
    url: url,
        success: function(data){
            $(selector).html(data);
        }
    });
}

ajax("file.php", "#mydiv");


How about just creating the script tag? Plus make sure the link is correct. Also, I heard that IE has some problems with dynamic script tags, so are you using IE?

$("<script src='js/script.js' type='text/javascript' ></script>").appendTo("head");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜