Jitendra Vyas
Jitendra Vyas

Reputation: 152647

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

Upvotes: 5

Views: 6209

Answers (1)

Daniel Vassallo
Daniel Vassallo

Reputation: 344301

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);
}

Upvotes: 5

Related Questions