1

I have two websites: domain1.com and domain2.com.

The script on domain1.com/external.php is:

<?php
echo <<<ots
<!--
document.write('Hello World!');
//-->
ots;
?>

I want to execute this external.php script on domain2.com, so I use:

<script src="http://domain1.com/external.php"></script>

The problem is - the Javascript often hangs out, so I wanted to include the < script.. right below the < /body>. However, I must print the Hello World! text in a specific place on the page (ie. right after the </head>).

Question - can I include the < script.. right below the < /body> to assign the output somehow and then put this variable on the page after the script executes?

Or any other similar solution? I cannot use JQuery.

  • 1
    Use `document.getElementById('idOfTarget').innerHTML='Hello World!';` [See also this answer](http://stackoverflow.com/a/17531159/1935077). – Petr R. Jul 26 '13 at 14:17
  • external.php I provided above is just an example - it is much more complicated than just printing the 'Hello World!' text. I MUST use the external.php script from domain1.com.. –  Jul 26 '13 at 14:18
  • Why do you need the `echo` part? – Naftali Jul 26 '13 at 14:19
  • `document.write` will _destroy_ the page's content if the document is already closed. You should consider Petr's suggestion. – Benjamin Gruenbaum Jul 26 '13 at 14:19
  • @PetrR. You should post as answer. Doesn't matter what the script do. Run the script, save the string in a variable, and print it as Petr R. said. Is easy and you should have no problems. – zozo Jul 26 '13 at 14:20
  • I need `echo` part because external.php is a PHP script. In general, it connects to MySQL database, takes the results (some texts) and prints it. –  Jul 26 '13 at 14:24

2 Answers2

0

You can but you have to use in your external.php file

header('Content-type: text/javascript');

So it's possible to use it like

<script src="http://domain1.com/external.php"></script>
The Alpha
  • 143,660
  • 29
  • 287
  • 307
0

You can use innerHTML to replace the content of one element on the page:

document.getElementById('idOfTarget').innerHTML='Hello World!';

Please note that you can modify the element only after it's created, so put the script before </body> if possible or use window.onload event:

window.onload=function(){
     document.getElementById('idOfTarget').innerHTML='Hello World!';
};

See also this mine earlier answer to see how to replace an element by the classname or the tagname.


If you need to use PHP in your script, then you still can do that:

window.onload=function(){
     document.getElementById('idOfTarget').innerHTML='<?php
          echo "Hello World!";
     ?>';
};

Please note that if you want to update a lot of elements, you may want to use AJAX/JSON.

Community
  • 1
  • 1
Petr R.
  • 1,247
  • 2
  • 22
  • 30
  • Thanks, but what about the external.php Javascript script part? (you may see my additional comment above). –  Jul 26 '13 at 14:30
  • @Tom - You still can use PHP without any limitations, I'll update my answer. – Petr R. Jul 26 '13 at 14:33
  • Ok, I think I get an idea and hopefully I can manage to implement it : –  Jul 26 '13 at 14:36
  • @Tom - Good luck! Feel free to ask for any clarification, if needed. Also, please consider [accepting the answer](http://meta.stackexchange.com/a/5235/224130) if this helped you. Thanks! – Petr R. Jul 26 '13 at 14:52