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.

6
  • 1
    Use document.getElementById('idOfTarget').innerHTML='Hello World!'; See also this answer. Commented Jul 26, 2013 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.. Commented Jul 26, 2013 at 14:18
  • Why do you need the echo part? Commented Jul 26, 2013 at 14:19
  • document.write will destroy the page's content if the document is already closed. You should consider Petr's suggestion. Commented Jul 26, 2013 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. Commented Jul 26, 2013 at 14:20

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, but what about the external.php Javascript script part? (you may see my additional comment above).
@Tom - You still can use PHP without any limitations, I'll update my answer.
Ok, I think I get an idea and hopefully I can manage to implement it :
@Tom - Good luck! Feel free to ask for any clarification, if needed. Also, please consider accepting the answer if this helped you. Thanks!
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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.