How to add javascript file in <head> in this condition?
Only if form
action is /?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.
<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?
And edit.js flie should be added after all other .js file
If you have to do it on the client-side in JavaScript, you may want to try the following:
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);
}
精彩评论