0

How can I inject a <script> to my document which will initialize a variable?

content_script:

$("body").append('<script type="text/javascript">myvar="hi there";</script>');

When I do this, the variable myvar is not recognized... If I add a simple alert(500); I get the alert.

Basically I just want to pass a variable from the extension the document to use it there.

musicman
  • 504
  • 6
  • 16
  • Covered in [Building a Chrome Extension - Inject code in a page using a Content script](http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script) – wOxxOm Nov 17 '15 at 17:36
  • actually you are right. jQuery can't do the job as it seems; but method 2 from your [link](http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script) does – musicman Nov 17 '15 at 18:06

1 Answers1

-1

add var

$("body").append('<script type="text/javascript">var myvar="hi there";</script>');
lordkain
  • 3,061
  • 1
  • 13
  • 18