Hari
Hari

Reputation: 137

How to load the url in html div tag using javascript or jquery

Here the below i have wriiten for load the webpage inside of div tag via 'url' i've enclosed. but it not working. can anyone know. pls help.

<html>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<a href="#" onClick="lurl();">Click Here</a>
<br/>
<br/>
<br/><div id="durl">
</div>
<script type="text/javascript">
function lurl(){
$('#durl').load('http://www.tndte.com/Result/');
}
</script>
</html>

Upvotes: 3

Views: 14316

Answers (4)

Samy
Samy

Reputation: 632

Before trying out the answers..You have to add http: in your src path.

Upvotes: 0

Dipesh Parmar
Dipesh Parmar

Reputation: 27354

Thats correct, ajax calls cannot retrieve external pages for security reasons.

The only way to get external pages onto your page is to use an iframe, or a simple server side proxy that you can call with your ajax.

We can not do that unless the content are coming from the same domain , and reason javaScript's Same Origin Policy.

You can still do it as

  1. use iframe load the content
  2. use server-side script

Upvotes: 1

Talha
Talha

Reputation: 19242

you can’t use jQuerys ajax methods to fetch data from external domains without using a Proxy, YQL, JSONP or equivalent technique to get around this. Browser restricts, most Ajax requests are subject to the "same origin policy".

You can use the https://github.com/padolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js and include the js file in your page. Below is the function that I used to load the external page.

function test () {
         $.ajax({
           url: 'http://www.tndte.com/Result/',
           type: 'GET',
           success: function(res) {
             var content = $(res.responseText).text();
             alert(content);
           }
         });
       }

Upvotes: 1

Edgar Allan Pwn
Edgar Allan Pwn

Reputation: 311

Unless you are accessing files on the same server as your page is, this will not work. You need to use an iframe if you're trying to embed external content.

http://www.w3.org/TR/html4/present/frames.html

Upvotes: 0

Related Questions