javascript 如何停止 js 插件在页面加载时运行。我只想修复 Ajax 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26057017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:25:48  来源:igfitidea点击:

How to stop pace js plugin to run on page load. I want to fix only on Ajax request

javascriptjqueryajax

提问by CodeBriefly

I'm using jquery pace pluginwith progress bar theme, all is working good. but i want to fix this run only on ajax request. After lots of searching i'm deciding to post there. Hope someone having the solution. Currently its running on both Ajax and page load.

我正在使用带有进度条主题的jquery 速度插件,一切正常。但我只想在 ajax 请求上修复此运行。经过大量搜索,我决定在那里发帖。希望有人有解决方案。目前它在 Ajax 和页面加载上运行。

Here's the js file call with options given by plugin. but no luck.

这是带有插件提供的选项的 js 文件调用。但没有运气。

<script data-pace-options='{ "document": false }' src="js/pace/pace.min.js"></script>

回答by CodeBriefly

I found the answer to stop pace.js on page load and here I'm posting this for other users.

我找到了在页面加载时停止 pace.js 的答案,在这里我将其发布给其他用户。

Here's the code of js calling.

下面是js调用的代码。

<script data-pace-options='{ "elements": { "selectors": [".selector"] }, "startOnPageLoad": false }' src="js/pace/pace.min.js"></script>

In this i'm setting pace to given selector and another option startOnPageLoad set to false to avoid loading pace on every page load.

在此,我将速度设置为给定的选择器,另一个选项 startOnPageLoad 设置为 false 以避免在每个页面加载时加载速度。

And here is the another question on pace.js which provide me a help.

这是关于 pace.js 的另一个问题,它为我提供了帮助。

Using pace.js on loading appended images

使用pace.js 加载附加图像

回答by Banana

If you do not want a script to run on page load, i can think of 2 options at the moment:

如果您不希望在页面加载时运行脚本,我目前可以想到 2 个选项:

1) open pace.min.js file and find the code that makes it run at start, and simply alter it into a function that you can call on your ajax call.

1) 打开pace.min.js 文件并找到让它在开始时运行的代码,然后简单地将其更改为一个可以在ajax 调用中调用的函数。

2) remove the pace.min.js link from header, and only attach it on your ajax request/response.

2) 从头中删除pacace.min.js 链接,并只将其附加到您的ajax 请求/响应中。

  • Hereyou can find an explanation on how to attach external code using javascript, for you it will be something like this:

    var fileref=document.createElement('script');
    fileref.setAttribute("data-pace-options","{ 'document': false }");
    fileref.setAttribute("src", "js/pace/pace.min.js");
    document.getElementsByTagName("head")[0].appendChild(fileref);
    
  • 在这里您可以找到有关如何使用 javascript 附加外部代码的说明,对于您来说,它会是这样的:

    var fileref=document.createElement('script');
    fileref.setAttribute("data-pace-options","{ 'document': false }");
    fileref.setAttribute("src", "js/pace/pace.min.js");
    document.getElementsByTagName("head")[0].appendChild(fileref);