Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7018705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:05:12+00:00 2026-05-27T23:05:12+00:00

I am making a cross domain request using script tag hack and jsonp. In

  • 0

I am making a cross domain request using script tag hack and jsonp. In the callback function, I want to write the data received to DOM using document.write().

I know that I should use appendChild/innerHTML instead of doc.write(). My constraint is I do not have a class/id hook to the element I want to write to. I can only rely on document.write() to write "in place".

Following code is included in HTML div in a script tag.:

function request_jsonp(){
var script = document.createElement('script');
var server_path = 'http://somedomain.com/?q=querystring&callback=request_callback';
script.setAttribute('src', server_path);
document.getElementsByTagName('head')[0].appendChild(script);
console.log('data requested');
// document.write('hello world'); // this gets written
}


function request_callback(data){
    console.log('data received');
    document.write(data);
}


 request_jsonp();

 window.onload = function(){
     console.log('onload fired');
 }


/* 
above code prints

data requested
data received
onload fired
*/

Basically document.write does not work even with a static string inside the callback function. I thought that if document.write is called before onload, it inserts text in the page as noted here JavaScript and HTML Script Tags

What is preventing the document.write() to write to DOM? Also, is there a better approach to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:05:13+00:00Added an answer on May 27, 2026 at 11:05 pm

    You should note that document.write(); calls an implicit document.open();, thus, in effect, clearing everything you have had in the document so far.
    It is impossible to add content to a document using document.write();. It is, however, possible to use document.write(); to write an entire document.

    You have several possible solutions:

    • You can pass the target element as a GET parameter, as @kirilloid noted.
    • You can insert an IFrame object at the desired place, and use myIFrame.document.write(); to update its content.

    This is basically happening because you are updating the content of a container that is the parent to your script code, and as such, is not closed yet.

    Either that, or I’m entirely off track here which, let’s face it, is entirely possible. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a cross-domain AJAX request using jQuery but my callback function is not
I am making a cross-domain JSON(P) call by using JavaScript to add a <script>
In an application where I am making cross-domain JSONP requests (using jQuery), when the
I'm trying to get a jsonp callback working using jquery within a greasemonkey script.
I am doing a cross domain request using $.ajax . It works on Firefox
I understand that for Silverlight to be able to make a cross-domain request using
I am making cross-domain Ajax calls using the latest version of jQuery 1.7.1. with
I'm making a cross domain POST request. I added Access-Control-* headers to the web
I am trying to make a simple cross domain data request. After receiving the
I want a simple and light-weighted way of making cross-domain requests to my api

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.