38188

in string ends script section

Question:

I'm having an issue where I'm trying to use JQuery to append a string to a div.

This string will be HTML, and occassionally users will be using the script tags inside the string.

Unfortunately, when the browser reads </script> inside the string, it ends the section and prints out all the following javascript into the browser. Obviously, we don't want that.

Is there a way to get the browser to not parse anything inside the string?

An example of this might be an Adsense ad being appended to the div.

Answer1:

You can use a backslash (\) to escape the slash in the <script> close tag so that it doesn't close the actual <script> element:

var foo = document.write('<script src="somewhere.js"><\/script>');

This is often used when loading a script from a CDN with a local fallback:

<script src="//cdn.example.com/jquery.js"></script> <script> if (!window.jQuery) document.write('<script src="/assets/js/jquery.js"><\/script>'); </script>

Answer2:

The workaround way for the problem is using:

"</scr" + "ipt>"

Ugly, annoying, but <strong>working!</strong>

Recommend

  • Is Google adsense breaking my menu?
  • Is Google adsense available for localhost?
  • Display Ads To % of Users
  • How can I place an Adsense ad in each hidden JQuery div without exceeding their 3 ad limit
  • How to show special escape characters like LineBreak in Java output?
  • Postgresql: Invalid regular expression: invalid backreference number
  • Can comments make any difference during the run-time?
  • Why does my regular expression fail with certain substitutions?
  • Insert Path of a file with \\\\ in mysql using java
  • Why does _GET in PHP wrongly decodes slash?
  • pandas parse csv with left and right quote chars
  • preg_replace
  • Add a div to replace Video after Video Plays Through
  • overlapping appointments using the entity framework
  • Recursion Control flow
  • Haskell: function composition with anonymous/lambda function
  • hibernate sets dirty flag (and issues update) even though client did not change value
  • Aptana 3 remove bundle (jquery)
  • How do I retrieve the user information of a user authenticated with Apache's mod_ldap?
  • In-place sed command not working
  • Uncaught TypeError: $(…).select2 is not a function
  • SAXReader not re-ecape characters
  • Appending Character to Character Array In C
  • Zurb Foundation _global.scss meta styles for js?
  • How do I open a C file with a relative path?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Master page gives error
  • HTML download movie download link
  • Controls, properties, events and timers running in design time
  • Updating server-side rendering client-side
  • SSO with signing and signature validation doesn't work
  • How to pass list parameters for each object using Spring MVC?
  • How to set the response of a form post action to a iframe source?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?