简体   繁体   English

重新加载jQuery和js库

[英]reload jquery and js library

I reload my page using an updatepanel. 我使用updatepanel重新加载页面。 In my masterpage I do the following. 在我的母版页中,执行以下操作。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>  
<script src="App_Themes/Project/js/core.js"></script>
<script src="App_Themes/Project/js/libs/modernizr-1.6.min.js"></script>

When I partially reload a page with ajax (updatepanels), this files are nog loaded. 当我用ajax(updatepanels)部分重新加载页面时,此文件将被加载。 So an slider is impossible to use. 因此无法使用滑块。

Is there a way to load this files when I do a Ajax call? 当我进行Ajax调用时,是否可以加载此文件?

You can use this function: 您可以使用此功能:

function ReloadScripts() {

    var scriptTag = document.getElementsByTagName('script');
    var src;

    for (var i = 0; i < scriptTag.length; i++) {
        src = scriptTag[i].src;
        scriptTag[i].parentNode.removeChild(scriptTag[i]);

        try {
            var x = document.createElement('script');
            x.type = 'text/javascript';
            x.src = src;
            //console.log(x)
            document.getElementsByTagName('head')[0].appendChild(x);
        }
        catch (e) {}
    }
};​

On ajax call success method, just call this function 在ajax调用成功方法上,只需调用此函数

$.ajax({
    type:"POST",
    dataType: 'json',
    error: function(data) {
        //do error stuff
    },
    success: function(data) {
        //do success stuff

        // at last call this
        ReloadScripts();
    }
});

Put all your initialization/re-initialization code in a js function call pageLoad() , it gets called on all async postbacks: 将所有初始化/重新初始化代码放在js函数调用pageLoad() ,在所有异步回发中都将调用它:

function pageLoad()
{
    //do work 
}

$(document).ready() and pageLoad() are not the same! $(document).ready()和pageLoad()不一样!

I think what you need is not to reload the js libraries, you need to call the exact function that applies some property to the some elements which are loaded by your ajax call. 我认为您不需要重新加载js库,而是需要调用将某些属性应用于由ajax调用加载的某些元素的确切函数。

For example: 例如:

Your ajax appends some links to page like 您的ajax将一些链接附加到页面上,例如

<a id="someLink">link</a>

And there is a code that colors this element into blue at a method inside a js library like: 在js库中的方法中,有一个代码将该元素着色为蓝色:

function colorBlue() {
    $("a").css("color", "blue");
}

So here you do not need to reload whole libraries, just call this function after your ajax call: 因此,这里您不需要重新加载整个库,只需在您的ajax调用之后调用此函数:

function makeAjax() {
    //here some ajax method is executed
    colorBlue();
}

And that's it, your needed changes are done inside the library method. 就是这样,您所需的更改是在库方法中完成的。 PS: You can find which method applies the specific work to your elements by searching the class, tag name or id of the target elements inside js library. PS:您可以通过在js库中搜索目标元素的类,标记名或ID来找到将特定工作应用于元素的方法。 There you 那里你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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