开发者

Opening a link in new tab or window when the user posts text using a textfield

I have a text area where the users of the system can post anything, even HTML tags. My problem is, when the user posts a link or an anchor tag to a page outside the domain of my website, the user leaves my domain and goes to another site. I don't want this to happen.

I want the link to be opened in a new tab or in a new window, so that the user still remains in my domain. How do I do this?

Here is how the scrap is being displayed:

            if($row2['html']==0)
        echo '<div id="scrap_text"&g开发者_如何学编程t;<pre>'.htmlentities($row2['scrap']).'</pre></div>';
        else
        echo '<div id="scrap_text"><pre>'.$row2['scrap'].'</pre></div>';
        echo '</div>';

And the jQuery is:

$(document).ready(function(){
                                    $("#scrap_text").find("a").attr("target", "_blank");  });

I am not getting the required functionality. The scraps displayed above using PHP, are loaded using placed AJAX. Is it because the div is loaded after the document.ready is being loaded?


Using JQuery, you can find <a> tags inside of a user post, and then inject the target="_blank" property into them.

Something like this:

$("#post").find("a").attr("target", "_blank");
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜