开发者

Go Link thru iFrame

I have problem and I tried click link then it doesn't work to open link using target: name of iFrame. i dont want use href because im going make show/hide div.

Javascript:

<script type="text/javascript">
<!--//
function godirect(url, targetname)
{
document.getElementById(targetname).src = url;
//开发者_Python百科frame[targetname].location.href = url;
}
//-->
</script>

in HTML and PHP:

$a=0;

echo '<a href="#" onclick="godirect("http://www.google.com", iframe_url'.$a.');">Click Me!</a>';

echo '<iframe class="iframe_url" id="iframe_url'.$a.'"></iframe>';


How about

<script type="text/javascript">
function godirect(url, targetname) {
  window.frames[targetname].location = url;
  //OR 
  //window.open(url,targetname);
  return false;
}
</script>

<?PHP 
$a=0;
?>
<a href="#" onclick="return godirect('http://www.google.com', 'iframe_url<? echo $a; ?>');">Click Me!</a>
<iframe class="iframe_url" name="iframe_url<? echo $a; ?>" id="iframe_url<? echo $a; ?>"></iframe>


You have to quote strings in JavaScript. You are trying to get the id of the element by passing in a variable which you haven't defined.

You are also using the same quote characters to delimit your HTML attribute value as you are using to delimit your JS strings.

To use the approach you are using, while making the minimum number of fixes to make it work:

echo '<a href="#" onclick="godirect(&quot;http://www.google.com&quot;, &quot;iframe_url'.$a.'&quot;);">Click Me!</a>';

Using JS for this is a very silly idea in the first place though, and your implementation fails to have any kind of fallback for when JS is not available (which is odd, since you are taking steps to stop browsers which don't recognise the script element from rendering the JS as content text).

You can do this with plain HTML:

<a href="http://www.google.com" 
   target="iframe_url<?php echo htmlspecialchars($a); ?>">
   Click Me!
</a>

i dont want use href because im going make show/hide div.

You can do that as well as having a normal, functioning link. Build on things that work.


Try this:

echo '<a href=\"#\" onclick=\"godirect(\"http:\/\/www.google.com\", iframe_url'.$a.');\">Click Me!</a>';
echo '<iframe class=\"iframe_url\" id=\"iframe_url'.$a.'\"></iframe>';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜