How do I add this javacript code into a php coded page!

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jul 2008
Posts: 4
Reputation: netnuta is an unknown quantity at this point 
Solved Threads: 0
netnuta netnuta is offline Offline
Newbie Poster

How do I add this javacript code into a php coded page!

 
0
  #1
Jul 30th, 2008
Hi there all!

I do not have a great knowledge of php but I do know basic html.

I have a javascript popunder script that I would like to try on my site that runs
on a php script.

I know how to insert this script into an html page but I can't get it to work in php.

I inserted the javascript that would go in the <head> of the html, before the <?php
tag. But i am not sure how to insert the <body onload='init()'> part of this into the php
page?

Can someone here possibly help me with this.

Thanking you in advance!

The full Javascript code is listed below:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <head>
  2.  
  3. <script type='text/javascript'>
  4. function doOpen(url)
  5. {
  6. if (get_cookie('popunder')==''){
  7. // attempt to open the popup
  8. win = window.open(url, 'test', 'toolbar,status,resizable,scrollbars,menubar,location,height=600,width=800');
  9. if (win)
  10. {
  11. // popup successfully created
  12. win.blur();
  13. setTimeout('win.focus();',10000);
  14. } else {
  15. // popup was not created.
  16. showPopupMessage(url);
  17. }
  18. var today = new Date();
  19. today.setTime( today.getTime() );
  20. expires = 1 * 1000 * 60 * 60 * 24;
  21. var expires_date = new Date( today.getTime() + (expires) );
  22. document.cookie="popunder=yes;expires=" + expires_date.toGMTString()
  23. }
  24. return win;
  25. }
  26. function init()
  27. {
  28. var win = doOpen('http://www.yoursite.com/');
  29. }
  30. function showPopupMessage(url)
  31. {
  32. if (!document.createElement)
  33. {
  34. return;
  35. }
  36. var elmDiv = document.createElement('div');
  37. if (typeof(elmDiv.innerHTML) != 'string')
  38. {
  39. return;
  40. }
  41. elmDiv.id = 'popupmessage';
  42. elmDiv.style.cssText =
  43. 'position: absolute; left: 300px; top: 225px;' +
  44. 'width: 350px;' +
  45. 'color: 000099; ' +
  46. 'background-color: white; ' +
  47. 'font-weight: bold; ' +
  48. 'font-size: 14; ' +
  49. 'border: solid black 2px; ' +
  50. 'padding: 1em;';
  51.  
  52. var html = 'Thank you for visiting this-site.com. ' +
  53. 'Please enjoy your stay. ' +
  54. 'Visit back tomorrow for more exciting news! ' +
  55.  
  56. '<p align="right">' +
  57. '<a href="#" ' +
  58. 'onclick="document.cookie=\'popunder=\'; win = doOpen(\'' + url + '\'); hidePopupMessage(); return false;">' +
  59. 'close</a>' +
  60.  
  61. '<\/div>';
  62. document.body.appendChild(elmDiv);
  63. elmDiv.innerHTML = html;
  64. }
  65. function hidePopupMessage()
  66. {
  67. var elmDiv = document.getElementById('popupmessage');
  68. if (elmDiv)
  69. {
  70. elmDiv.parentNode.removeChild(elmDiv);
  71. }
  72. }
  73. function get_cookie(Name) {
  74. var search = Name + "="
  75. var returnvalue = "";
  76. if (document.cookie.length > 0) {
  77. offset = document.cookie.indexOf(search)
  78. if (offset != -1) { // if the cookie exists
  79. offset += search.length
  80. end = document.cookie.indexOf(";", offset); // set the index of beginning value
  81.  
  82. if (end == -1) // set the index of the end of cookie value
  83. end = document.cookie.length;
  84. returnvalue=unescape(document.cookie.substring(offset, end))
  85. }
  86. }
  87. return returnvalue;
  88. }
  89. </script>
  90.  
  91.  
  92. </head>
  93.  
  94. <body onload='init()'>
Last edited by Tekmaven; Jul 31st, 2008 at 6:31 am. Reason: Code tags
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is online now Online
Veteran Poster

Re: How do I add this javacript code into a php coded page!

 
0
  #2
Jul 31st, 2008
You just put all your javascript statements in echo like following:
  1. echo'<script language="javascript">window.location.href="example.php";</script>';
Last edited by Shanti Chepuru; Jul 31st, 2008 at 4:33 am.
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 18
Reputation: jel3 is an unknown quantity at this point 
Solved Threads: 3
jel3's Avatar
jel3 jel3 is offline Offline
Newbie Poster

Re: How do I add this javacript code into a php coded page!

 
0
  #3
Aug 1st, 2008
use print in stead like:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. print '<script language="javascript">window.location.href="example.php";</script>';
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 4
Reputation: netnuta is an unknown quantity at this point 
Solved Threads: 0
netnuta netnuta is offline Offline
Newbie Poster

Re: How do I add this javacript code into a php coded page!

 
0
  #4
Aug 2nd, 2008
Hi thank's for help!

It is great to get a response to my post as I have posed the same query on dp a few times with no response at all!

Many thank's to you all!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC