4

I research on google before post on Stackoverflow,, But all problem that i have found are different then my problem.

I am Developing Chrome Extension and load JS from JS...

I am placing an ad script into HTML head..

if i write a script manually in HTML head

<script src="Ads_URL"></script>
Its Working ads show..

. But if i append script throung Jquery or Pure javascript its return Warning

$("head").append("Google Analythic Script"); // Working
$("head").append("<script src="Ads_URL"></script>");//Return warning

Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.

Any solution?

Update

This question is not duplicate.. Link in first comment is not helpful for me.. In that question @jfriend00 is using document.write("<span>"); If i use document.write() function, Ads show, but remove all tags from body and just <script> is left "document.write() Clear the body"

Here is my script that i want to inject

<script data-cfasync=false src='//s.ato.mx/p.js#id=21352&size=300x250'></script>
Asad Raza
  • 34
  • 1
  • 2
  • 14
  • 4
    have you read this? http://stackoverflow.com/questions/24297829/execute-write-on-doc-it-isnt-possible-to-write-into-a-document-from-an-asynchr – Provie9 Sep 01 '16 at 19:24
  • 1
    In addition to being a duplicate, this is off-topic because it is a debugging question that does not provide a [mcve] (the code being added via `src="Ads_URL"`). Questions seeking debugging help ("**why isn't this code working?**") must include: ►the desired behavior, ►a specific problem or error *and* ►the shortest code necessary to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: "**How to create a [mcve]**", [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Sep 01 '16 at 20:24
  • If you believe the question linked in the first comment here is not a duplicate, please, in your question, link to that other question, explicitly state that you don't believe it is a duplicate, and why it is not . – Makyen Sep 01 '16 at 20:29
  • I update my question, it is not duplicate... Please check – Asad Raza Sep 02 '16 at 04:53
  • @AsadRaza what is the solution? I need it. – Milor123 Nov 21 '17 at 22:46
  • @milor123 sorry i forget what i tried, But i think i tried document.createElement('script'); , -- You can google "js include another js file" OR "document.createElement('script')", here is 2 links https://stackoverflow.com/questions/4634644/how-to-include-js-file-in-another-js-file ------ second link: https://stackoverflow.com/questions/950087/how-do-i-include-a-javascript-file-in-another-javascript-file – Asad Raza Nov 22 '17 at 06:54

1 Answers1

1

Did you tried this: chrome.tabs.executeScript? This didn`t add script in html but execute same as console. You can try to get contents of remote javascript code as a string and execute it.

Artem Gorlachev
  • 597
  • 4
  • 9
  • I cant do this, Because i have not only Ads URL, i have random script in parameter, some time it is script with variables functions, and some time it is external url script. and both at a time.. – Asad Raza Sep 02 '16 at 19:32