views:

54

answers:

1

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

+3  A: 

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);
}
Daniel Vassallo
Is it 100% certain that the browser will pick up the DOM change and load the script?
Mikael Svenson
@Mikael: Yes. Dynamic script insertion is a commonly used pattern. It is also used for [JSONP](http://en.wikipedia.org/wiki/JSON#JSONP).
Daniel Vassallo
@Daniel: I've inserted scripts before, and then called them, but didn't know that loading also worked. Cool :D
Glen
@Daniel Vassallo - Ok i will try this
metal-gear-solid
@Daniel Vassallo - will it give error if action="/" is something else?
metal-gear-solid
@metal-gear-solid: No it shouldn't. `document.getElementById('main').action` should give you the absolute URL of the action, if one is set. We used `indexOf` and not `==`, because of the absolute url.
Daniel Vassallo