开发者

jQuery Loading external content in Div, link problem

I've loaded content from an external site into a Div with jQuery, but when I click on a link in the loaded content it opens as it's own page. Is there a way restrict 开发者_开发知识库the link to opening the page while having it still contained within the Div?


The only way to remove that is to set the "href" attr of all the links to "#", as I think. Please find the code below for the same.

$("#div").find("a").attr("href", "#")

Hope this is useful!!


You will have to use iframe instead of div for an easy solution.

Alternatively, since you are saying the content is from external site, I believe you know how to get the content across different domain. You can try this.

$("divContainer a").click(function(e){

    //Prevent the default behavior of the link so it will not redirect to href.
    e.preventDefault();

    var url = this.href;

    //Now using this url get the external content and load it into the DIV

});


You have at least 3 troubles there:

  1. Possible copyright law violation if you don't have permission to use the materials.

  2. A security violation. All AJAX requests must usually come from the same server. Unless the user and the ISP severely reduce their security settings, anything else is suspected as malicious code by the security features.

  3. Windows and frames are made to contain web pages. Div tags are not. You will have to strip the file to only some inner-body content to display it in a div. A popup is a window, not a div.

Use the IFrame suggestion from ShankarSangoli

This is the best solution.


You could just remove the target attribute in the loaded contents links::

$("#div").find("a").attr("target", "");

This should prevent the links from opening in a new window.

Question though, how are you getting content from another site using jQuery? That should not be possible?


you clearly should provide some source code in order to give us a clue of what's going on.

IN my experience (self taught jquery noob) the .load thing has lots of issues, some times you solve it with a "return false;" at the end of all the commands in order to "prevent default" interaciton.

Other times you just got to clean the code like html tags... it could be everything, provide code.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜