943,829 Members | Top Members by Rank

Ad:
Jul 30th, 2008
0

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

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
netnuta is offline Offline
4 posts
since Jul 2008
Jul 31st, 2008
0

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

You just put all your javascript statements in echo like following:
javascript Syntax (Toggle Plain Text)
  1. echo'<script language="javascript">window.location.href="example.php";</script>';
Last edited by Shanti C; Jul 31st, 2008 at 4:33 am.
Reputation Points: 137
Solved Threads: 162
Posting Virtuoso
Shanti C is offline Offline
1,641 posts
since Jul 2008
Aug 1st, 2008
0

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

use print in stead like:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. print '<script language="javascript">window.location.href="example.php";</script>';
Reputation Points: 12
Solved Threads: 3
Newbie Poster
jel3 is offline Offline
18 posts
since May 2008
Aug 2nd, 2008
0

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

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!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
netnuta is offline Offline
4 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Select click or change
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: How to call a javascript function other than form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC