By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

Is there a way you can include code from another document?

Blue1244
P: 39
Is there a way you can include code from another document like you can with PHP?
PHP way..
Expand|Select|Wrap|Line Numbers
  1. <?php include('name of document.php'); ?>
Can you do something similar with javascript? If so can you plz help me...
Sep 20 '13 #1

✓ answered by Dormilich

first, <center> is long deprecated.

second, that’s not what code inclusion is intended for.

nevertheless, there are several other methods to do that.
  • make a function
  • get the HTML from the server using AJAX
  • get the data from the server (AJAX again) and put it through a templating engine

Share this Question
Share on Google+
6 Replies


Dormilich
Expert Mod 5K+
P: 8,639
Can you do something similar with javascript?
depends what you expect the included code to do. right off the bat I know require() (doc) and in the browser you can dynamically load scripts as well (keyword: dependency management).
but certainly it works somewhat different than PHP’s include. e.g. you cannot include output, only scripts.
Sep 20 '13 #2

Blue1244
P: 39
so with what you are talking about can i bring in code from another page to paste a button and a form box onto a page? I'm trying to make that game i was making earlier better(just in case you remember)but, if the answer is correct it creates the next textbox and enter button. is this possible using what you are talking about?
Sep 20 '13 #3

Blue1244
P: 39
This is the code I have so far,
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html>
  3. <center><h3>What is an example of a unit rate?</h3></center>
  4. <center><p>A. A ratio showing how many pickles are canned in 4 hours.</p></center>
  5. <center><p>B. A Ratio showing how many problems solved in 1 minute.</p></center>
  6. <center><p>C. A ratio showing how many days it takes to grow one pepper.</p></center>
  7. <center><h5>(Write answers in Caps without the period.)</h5></center>
  8. <center><form><input type="button" value="Answer" onClick="one()"></input></form></center>
  9. <script type="text/javascript">
  10. function one() {
  11. var a= prompt('What is your choice? A, B, or C?');
  12. if(a==B) {
  13. var b= alert('Correct!');
  14. if(b==true) {
  15. require('problem2input.html');
  16. }
  17. }
  18. else {
  19. var d= confirm('Incorrect!');
  20. if(d==true) {
  21. location.reload();
  22. }
  23. else {
  24. location.reload();
  25. }
  26. }
  27. }
  28. </script?
  29.  
This is the code i need to import,
Expand|Select|Wrap|Line Numbers
  1. <center><h3>What is PI used for?</h3></center>
  2. <center><p>A. Area of a Triangle, Circumference, and volume of a box.</p></center>
  3. <center><p>B. Area of a square, area of a circle, and perimeter of a triangle.</p></center>
  4. <center><p>C. Are of a Circle, Circumference, and volume of a cylinder.</p></center>
  5. <center><h5>(Write answers in Caps without the period.)</h5></center>
  6.  
  7.  
  8.  
  9. <center><form><input type="button" value="Answer" onClick="two()"></input></form></center>
any help and the button isn't working I'll figure it out tomorrow i'm tired...
Sep 20 '13 #4

Dormilich
Expert Mod 5K+
P: 8,639
first, <center> is long deprecated.

second, that’s not what code inclusion is intended for.

nevertheless, there are several other methods to do that.
  • make a function
  • get the HTML from the server using AJAX
  • get the data from the server (AJAX again) and put it through a templating engine
Sep 20 '13 #5

Blue1244
P: 39
I'll just close this discussion and re ask another question on how to use php to use information from a button... i'll just try to figure out how to do all this stuff i need to do with javascript with the php server side code...
Thank you for the help anyways.. :)
Sep 20 '13 #6

Dormilich
Expert Mod 5K+
P: 8,639
and re ask another question on how to use php to use information from a button
strictly speaking, not at all. there isn’t much information you could put in a button, and on the design side, it doesn’t even make sense to do so.
Sep 21 '13 #7

Post your reply

Sign in to post your reply or Sign up for a free account.