简体   繁体   中英

How do I create a link that opens up a new window with PHP?

I'm currently attempting to create a link like this one:

<a href="http://www.mymra.com" onClick="mywindow=window.open('http://www.mymra.com','mywindow','toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width=900,height=600'); return false;">Motorcycle Insurance</a></li>

in PHP. I've attempted to do so with a link in one of my php files, however I can't get it to work correctly, nor load without any errors.

This is the code I've been working with:

        echo '
  <a href="'.$ad_link.'" onClick="javascript: pageTracker._trackPageview(\'/outgoing/'.$ga_link.'\'); mywindow=window.open('.$ad_link.');" target=\"_blank\" title="'.$ad_alt.'"'.$ad_target.'>
  <img src="http://'.$ad_image.'" border="0" alt="'.$ad_alt.'" width="150px" height="150px" /></a>';

I'm curious as to what I need to do to get the link to work.

I've already tried:

  • Copy and pasting the code exactly. (with the exception of editing the links)
  • Snipping the code over, taking out anything I thought could cause an error.
  • Re-assembling the code to make it work.

I'm unable to create a solution myself, so I'm turning to the community. If anyone could at least lead me in the right direction I would appreciate it.

The php code snippet I provided above is not necessary. I just need an example piece of code to work with.

Thank you for helping me!

I've attempted to use:

echo '
<a href="'.$ad_link.'" onclick="pageTracker._trackPageview(\'/outgoing/'.$ga_link.'\'); mywindow=window.open('.$ad_link.'); return false;" target=\"_blank\" title="'.$ad_alt.'"'.$ad_target.'>
<img src="http://'.$ad_image.'" border="0" alt="'.$ad_alt.'" width="150px" height="150px" /></a>';

However it then treats the // after http: as a comment tag instead of an addition to http: This was one of my problems previously.

I'm also looking for a lightbox, however for future reference I need help answering this.

try:

  echo '
  <a href="'.$ad_link.'" onclick="pageTracker._trackPageview(\'/outgoing/'.$ga_link.'\'); mywindow=window.open('.$ad_link.'); return false;" target=\"_blank\" title="'.$ad_alt.'"'.$ad_target.'>
  <img src="http://'.$ad_image.'" border="0" alt="'.$ad_alt.'" width="150px" height="150px" /></a>';

I don't know what $ad_target is supposed to contain and therefore if it can be placed there, anyway: starting onclick with javascript: is incorrect, and furthermore you lack return false; at the end of the onlick .

Try to view echoed html and check for errors in there, any js console (ie from Chrome or Firebug) may be helpful as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM