最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to add javascript file in <head> in this condition? - Stack Overflow

programmeradmin4浏览0评论

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

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

Share Improve this question asked Sep 3, 2010 at 12:03 Jitendra VyasJitendra Vyas 153k240 gold badges587 silver badges868 bronze badges 2
  • 1 Why don't you handle this on the server-side? – Daniel Vassallo Commented Sep 3, 2010 at 12:04
  • because i don't have those rights. i need this for particular site on multi-site CMS – Jitendra Vyas Commented Sep 3, 2010 at 12:05
Add a ment  | 

1 Answer 1

Reset to default 5

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);
}
发布评论

评论列表(0)

  1. 暂无评论