0

I have the following in my source HTML:

<head>
    ...
    <script>window.jQuery || document.write('<script src="/js/jquery.min.js"></script>');</script>
    ...
</head>

But it is getting interpreted by my browser (Chrome) as:

<head>
    ...
    <script>window.jQuery || document.write('<script src="/js/jquery.min.js"></script>
</head>
<body>
    "');"
    ...

I've tried escaping the slash inside the document.write string, but that didn't work. Does anyone know how to prevent the browser from interpreting it as as a closing script tag?

2 Answers 2

5

</script> is alwasy interpreted as closing script tag even if it's inside string you need to split it like this:

'</'+'script>';
Sign up to request clarification or add additional context in comments.

Comments

2

You need to escape some characters in your script.

Try this:

<script>window.jQuery || document.write('<script src="/js/jquery.min.js"><\/script>')</script>

I escaped the / character with a backslash \, so it ends up like: \/ in your document.write() statement.

For a good example of this setup, try looking at HTML5 Boilerplate project on GitHub here:

https://github.com/h5bp/html5-boilerplate/blob/master/src/index.html#L26

Hope this helps.

1 Comment

I thought I had tried that, but I guess I'd have to escape the escaping backslash since I'm using Jade

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.