Kris
Kris

Reputation: 95

How to get an href from an external website?

I'm trying to get an href element from an external site for which I am not the administrator.

The code of this href is like:

<a href="?form=397&amp&id=45950&amp&act=act">Action</a>

I want only to get a: '?form=397&amp&id=45950&amp&act=act'.

How can I do that? Maybe using JQuery?

Upvotes: 1

Views: 385

Answers (2)

chris-l
chris-l

Reputation: 2841

Well, since you edited your answer to say:

How can I do that? Eg. using JQuery. It`s just eg.

Since using jquery was only eg, here is a non-jquery solution:

$ curl 'http://pastebin.com/raw.php?i=hDPyZ2Ne' |sed -n 's/.*href="\([^"]*\).*/\1/p'

(replace that url for the url of your site)

That line would be executed on a *nix terminal. That will output

?form=397&amp&id=45950&amp&act=act

Now, is completly impossible to do it with jquery? The way you mean it, yeah, but if you use phantomjs, you could execute your own js over the page DOM.

And here is a phantomjs script which uses jquery to do it:

var page = require('webpage').create(); 
var url = 'http://...put your url here';      
page.open(url, function() {
    page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js", function() {
        var href = page.evaluate(function() {
            return $('a').attr('href');
        });
        console.log(href);
        phantom.exit();
    });
});

EDIT: The method Front End Guy gave you is pretty good too.

Upvotes: 0

Lost Left Stack
Lost Left Stack

Reputation: 1952

You're going to run into issues because of cross-domain browser restrictions. There is an ajax plugin that you could use if you want to use JS:

https://github.com/padolsey/jQuery-Plugins/blob/master/cross-domain-ajax/jquery.xdomainajax.js

then just use:

$.ajax({
    url: 'http://www.somewebsite.com',
    type: 'GET',
    success: function(res) {
        // your code here
    }
});

Upvotes: 2

Related Questions