0

I'm trying to append the following string to head with jQuery :

"<script type='text/javascript'> window['adrum-app-key'] = 'dummy';</script>"

But it always fails. Trying to do the same with 'Hello' string for example works as expected. Following the code snippet I use to append the string :

var integrationScriptTag = handlebars.partials.integration(integrationData);
    $(document).ready(function() {
        $('head').append(integrationScriptTag.trim());
    });

First string is the result of parsing integrationData. Any help will really be appreciated.

Edit: I realize I misguided some of you with the first string. It is just a representation of what is produced by the first line of the second code snippet. So it doesn't really matter if it is some quote marks or not. The fact is I don't use a literal but rather a variable which is equal to the first string. I corrected the syntax so that there is no more confusion. Here is the jsFiddle reproducing the problem. jsFiddle

P. Ekouaghe
  • 214
  • 5
  • 10
  • 3
    you need to escape the embedded single quotes inside your string. – Mike Corcoran Dec 03 '14 at 17:47
  • I don't know why you mark it as off-topic. The problem is easily reproducible and is not resolved by simple typographical fix. Here is a jsFiddle simulating the problem [link](http://jsfiddle.net/8u8qf01e/). – P. Ekouaghe Dec 04 '14 at 08:47

4 Answers4

0

This should fix it:

'<script type=\'text/javascript\'> window[\'adrum-app-key\'] = \'dummy\';</script>'

or this would too:

'<script type="text/javascript"> window["adrum-app-key"] = "dummy";</script>'

Essentially you have quotes mismatch.

charles
  • 547
  • 1
  • 3
  • 11
0

Use this, it will fix the issue

'<script type=\'text/javascript\'> window[\'adrum-app-key\'] =\'dummy\';<\/script>'
Biswas
  • 598
  • 2
  • 16
  • 34
0

I finally found that you cannot append script tags to head with jQuery this way. I found some solutions here and there (look at the first code snippet of the selected answer for the second link).

Community
  • 1
  • 1
P. Ekouaghe
  • 214
  • 5
  • 10
-1

The head is parsed prior to the execution of javascript. So you probably have to perform the append outside the $(document).ready function.

Verhaeren
  • 1,661
  • 9
  • 10