简体   繁体   English

如何添加javascript文件 <head> 在这种情况下?

[英]How to add javascript file in <head> in this condition?

Only if form action is /?cms_mode=edit 仅当form操作是/?cms_mode=edit

<body id="home">
    <form method="post" action="/?cms_mode=edit" id="main">
    </form>
</body>

then a js file edit.js should be added into head, otherwise not. 然后应该将js文件edit.js添加到head中,否则不会。

<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript" src="edit.js"></script>
</head>

Is it possible through jquery/javascript? 可以通过jquery / javascript吗?

And edit.js flie should be added after all other .js file 并且应该在所有其他.js文件之后添加edit.js flie

If you have to do it on the client-side in JavaScript, you may want to try the following: 如果您必须在JavaScript中在客户端执行此操作,则可能需要尝试以下操作:

var newScript;

if (document.getElementById('main').action.indexOf('?cms_mode=edit') >= 0) {  
   newScript = document.createElement('script');
   newScript.type = 'text/javascript';
   newScript.src = 'edit.js';
   document.getElementsByTagName("head")[0].appendChild(newScript);
}

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

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