0

I can´t get works this with jquery:

<a href="#" onclick='jQuery('#load_url').show(1000).load('http://google.es');'>Google</a>

I need when i do click over this link load the url inside div called #load_url

All time happend the same thing and no load never the url if i test this with one function works but i need works inside link and at the moment i try all kind of things and never get works fine

Cœur
  • 37,241
  • 25
  • 195
  • 267

3 Answers3

0

Yes, the problem is the cross-origin policy. I have created a fiddle, it logs the error to the browser console: Cross-Origin request blocked. Any you can't solve this using iframe either, as Google blocks external framing request:

Load denied by X-Frame-Options: 
https://www.google.es/?gws_rd=ssl does not permit cross-origin framing.
meskobalazs
  • 15,741
  • 2
  • 40
  • 63
  • Google it´s only example can be other url the most important it´s if i writte ok this expresion with the link – Francisco Gonzalez Jan 03 '15 at 12:05
  • Well, most sites disallow cross-origin requests, if the target is your own site (or any other site, where you may change the policy), its okay, otherwise this won't work. – meskobalazs Jan 03 '15 at 12:06
0

If this is only for testing purposes, you can use this:

function myFunc() {
    
    $.getJSON('http://anyorigin.com/dev/get?url=google.es&callback=?', function(data){
 $('#load_url').show(1000).html(data.contents);
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='load_url'></div>

<a href="#" onclick="myFunc()">Google</a>

This uses http://anyorigin.com/ to bypass the Same Origin Policy restriction.

So for using it a few times, this would work, but as it says on the website, too much use can cause you to be banned.

See here for ways to bypass the policy.

If in the real application, you link to a page on your site, you can simply use what you were using before:

<a href="#" onclick="jQuery('#load_url').show(1000).load('localpage.html');">local page</a>
Community
  • 1
  • 1
ᔕᖺᘎᕊ
  • 2,971
  • 3
  • 23
  • 38
0

Syntaxically your tag is incorrect, your break the main string ; replace the outer quotes by double quotes, and use a local page (create a test.html page in the same directory for example, and try to load this one instead of an external website) :

<a href="#" onclick="jQuery('#load_url').show(1000).load('test.html');">Google</a>
sodawillow
  • 12,497
  • 4
  • 34
  • 44