简体   繁体   English

document.ready可通过单个JavaScript文件实现多种形式

[英]document.ready for multiple forms with single JavaScript file

I am referencing JavaScript as follows on an HTML page: 我在HTML页面上引用JavaScript如下:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.1.min.js"></script>    
</script>
<script type="text/javascript" src="js/application.js"></script>
<script type="text/javascript">
$('document').ready(function() {   
// call some function specific to this page 
alert("this alert will only show for this particuar page");
});
</script>

My question is I have lot of functions especially bootstrap/jquery plugin functions to go inside document.ready(). 我的问题是我有很多功能,尤其是bootstrap / jquery插件功能可以放入document.ready()中。 As a result, there is lot of javascript code in all my html pages. 结果,我所有的html页面中都有很多javascript代码。 How can I port all my functions that need to be executed inside document.ready to shared file application.js? 如何将需要在document.ready内部执行的所有功能移植到共享文件application.js中? I know that I can write a giant document.ready inside application.js to has all dom ready functions for all html files inside it. 我知道我可以在application.js内编写一个巨大的document.ready,以为其中的所有html文件提供所有dom ready功能。 Is there any better option? 有没有更好的选择? Thank you for your help. 谢谢您的帮助。

Edit: 编辑:

Sorry if I was not clear. 对不起,如果我不清楚。 I was meaning to say: if there are two html pages: html1, html2 how do I tell my shared JavaScript file to execute document.ready functions for html1 and not for html2. 我的意思是说:如果有两个html页面:html1,html2,如何告诉我的共享JavaScript文件执行html1而不是html2的document.ready函数。 If I understand it correctly, all functions inside document.ready will get executed every time a page loads. 如果我理解正确,则每次页面加载时都会执行document.ready内部的所有功能。

Similar to my situation here 类似于我在这里的情况

The best idea would be to move all your JS to application.js, and let this find out on which page it is. 最好的主意是将所有JS移到application.js,然后让它找出它在哪一页上。

The most simple solution is to look at the URL of the current page: 最简单的解决方案是查看当前页面的URL:

// jQuery
$(document).ready(function() {  
    if(document.location.pathname.matches(/your-page\.html/)) {
          // do someting
    }
});

But a more elegant and reusable solution is to look for specific elements you want to interact with: 但是,一种更优雅且可重用的解决方案是查找要与之交互的特定元素:

// jQuery
$(document).ready(function() {  
    var someSpecialElement = $('#some-special-element');
    if (someSpecialElement.length) {
          // do someting
    } 
    var someOtherSpecialElement = $('#some-other-special-element');
    if (someOtherSpecialElement.length) {
          // do someting
    } 
});

or… 要么…

// Shiny Javascript
(function(){
    const someSpecialElement = document.querySelector('#some-special-element');
    if (someSpecialElement.length) {
          // do someting
    } 
    const someOtherSpecialElement = document.querySelector('#some-other-special-element');
    if (someOtherSpecialElement.length) {
          // do someting
    }
})();

Or you can put classes on your body-Tag and test after the HTML has loaded 或者,您可以将类放在body-Tag上,然后在HTML加载后进行测试

// jQuery
$(document).ready(function() {  
    var body = $('body');
    if (body.hasClass('edit-some-stuff')) {
          // do someting
    } 
    if (body.hasClass('delete-some-stuff')) {
          // do someting
    } 
});

or … 要么 …

// Shiny Javascript
(function(){
    const body = document.querySelector('body');
    if (body.classList.contains('edit-some-stuff')) {
          // do someting
    } 
    if (body.classList.contains('delete-some-stuff')) {
          // do someting
    } 
})();

You can use script defer attribute, it will make all your scripts load AFTER html is renderer. 您可以使用script defer属性,它将使所有脚本在html是渲染器之后加载。 So it's equal to put all scripts under document ready. 因此,等于将所有脚本都准备就绪。 Here is the link: 链接在这里:

http://www.w3schools.com/tags/att_script_defer.asp http://www.w3schools.com/tags/att_script_defer.asp

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

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