2

I need to make a function in JavaScript to locate href inside the url that is given, and to return it as a string.

For example: http://stackoverflow.com/

So the function starts with: function example(url) {}
I want to find the first link inside this url that contain the words google.

In this page there is somewhere link like <a href:"http://google.com/asdasdadsa/asdada">
The function is to return the whole link as string.

timss
  • 9,982
  • 4
  • 34
  • 56

1 Answers1

3

So basically from what I can gather, you want to look at each link on the page and get the whole URL if it includes some string (i.e. google).

Here's a function that finds the first link matching a certain string:

function checkLinks( searchString ) {
    var url;

    // Go through each link
    $('a').each( function ( ) { 

        // Check if the search string exists
        if(  $(this).attr('href').indexOf(searchString) != -1 ) { 
            url = $(this).attr('href');
            // If we've found one, stop the each. 
            return false;
        }
    });
    return url;
}

I've put together a jsfiddle showing an example of how this function could be used:

http://jsfiddle.net/K9KvS/1/

EDIT:

I've just seen you need to do this on a remote URL. You probably need to use AJAX to load in the code, then run this on the code you have. Unfortunately due to the same origin policy, you can't get this directly, so you'll need to run a server-side script on your server (e.g. using PHP) to load the content of the external page, then an AJAX call from your JS to pull it into your javascript.

Modified version to include an AJAX load of some code, then a find on that code:

// Create a function to do the actual search
function checkLinks( code, searchString ) {
    var url;
    // Search the code for all <a> tags, the loop over them
    $(code).find('a').each( function ( ) {

        // Check if there is a match (indexOf returns -1 if not)
        if(  $(this).attr('href').indexOf(searchString) != -1 ) {

            // set the "url" variable to the href
            url = $(this).attr('href');
            // Stop looping
            return false;

        }
    });
    return url;
}

// Now, when the page loads, attach an AJAX call to a button with ID "linkchecker"
$( function ( ) {
    $('#linkchecker').click( function( ) {
        var code;

        // Perform the AJAX call, load the data and call our function above to find "google.com"
        $.get( 'load_code.php?url=http://www.google.com', function( data ) {
            code = data;
            alert( checkLinks( code, 'google.com' ) );
        });
    });
});

load_code.php would probably look something like this (probably with some error checking, etc):

<?php
    $htm = file_get_contents($_GET['url']);
    echo $htm;
?>

Update: Using Raw Javascript

We'll modify checkLinks from above to use raw Javascript:

function checkLinks( code, searchString )
{

    var url;

    // We need to create an HTML document element so we can use javascript dom functions on it.
    var doc = document.createElement("html");
    doc.innerHTML = code; // put the code into the document

    // Get all  links in the code
    var links = doc.getElementsByTagName("a")

    // Loop over all links
    for (var i=0; i<links.length; i++) {
        // Check if the search string (e.g "google.com") is found in the href of the link
        if(  links[i].getAttribute("href").indexOf(searchString) != -1 ) {
            // Set it to the return value
            url = links[i].getAttribute("href");
            // stop looping
            break;
        }
    }

    return url;
}

So firstly, you need to set up the Ajax request object. The problem is this differs between browsers, so you need an unpleasant bit of code to generate it across them. The following is modified from the tiztag ajax tutorial:

function makeAJAXObject(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    return ajaxRequest;
}

Ok, so now we've got our AJAX object, we want to get it to load a page, and tell it how to handle what we get back:

/*
 * A function to load a given URL and process the code from it.
 * It takes three arguments:
 *   php_handler   The name of the PHP file that will load the code (or ASP, or whatever you choose to use) 
 *   url           The URL to be loaded.
 *   searchString  The string to find in the links (e.g. "google.com").
 */
function load_page( php_handler, url, searchString )
{
    // Get the ajax object using our function above.
    window.ajax = makeAJAXObject( );

    // Tell the AJAX object what to do when it's loaded the page
    window.ajax.onreadystatechange = function(){
        if(window.ajax.readyState == 4){ // 4 means it's loaded ok.
            // For simplicity, I'll just alert this, but you would put your code to handle what to do when a match is found here.
            alert(checkLinks( window.ajax.responseText, searchString ));    
        }
    }

    // Set up the variables you want to sent to your PHP page (namely, the URL of the page to load)
    var queryString = "?url=" + url;
    // Load the PHP script that opens the page
    window.ajax.open("GET", php_handler + queryString, true);
    window.ajax.send(null); 
}

The final thing is to attach this to a button when the page has loaded:

window.onload = function( ) {
    document.getElementById('linkchecker').onclick = function( ) {
        load_page('load_page.php', 'http://www.example.com', 'google');
    }
}

Please note, there's likely to be built in WinJS functions to handle some of the AJAX stuff, but I've never tried Win 8 app development, so I don't know them!

n00dle
  • 5,949
  • 2
  • 35
  • 48
  • Thank you for your answer, Unfortunately I can't use jquery in the win 8 app development feathers is not including jqurey this one reason why I could't do it, but maybe there is a way to pass that around I will look. Thank you again for your answer. – Daniel Iliaguev Harta May 30 '13 at 03:54
  • In which case, you should remove jQuery from the tags :) You can do the same using raw Javascript, see this link for some help: http://stackoverflow.com/questions/1487588/extract-all-links-from-a-string – n00dle May 30 '13 at 03:56
  • Thank you very much! I working right now to make it raw javascript (not easy for me but I don't complain ), if you could just make a little explanation of the script so I could do it myself next it would be grate ! AND once again THANK YOU ! – Daniel Iliaguev Harta May 30 '13 at 04:33
  • I've updated it with all the raw JS you'd need and commented it up. If you need any more help, let me know. – n00dle May 30 '13 at 14:12
  • n00dle I have no words to tell you how much I am thankful to you, I never thought you will go so far just to help someone, that's just grate ! All I can say is THANK YOU VERY MUCH! I will try it right now . – Daniel Iliaguev Harta May 31 '13 at 09:48
  • n00dle I am having a problem with the function, after few cheekiness seems like the problem is around the "onreadystatechange", looks like the function is never called also tested the makeAJAXObject( ) seems to work alright, any idea where can be the problem ? – Daniel Iliaguev Harta May 31 '13 at 11:07
  • Sounds like your Page download script might be hanging. If you want to track the proress of the ajax call in more detail you can monitor for the other statechange numbers: 0: request not initialized, 1: server connection established, 2: request received, 3: processing request. Just put if statements near where you check the page is ready. Also try running your page download script separately and see if it manages ok. – n00dle May 31 '13 at 11:37
  • The change state was on 0 until I moved the call before the onreadystatechange now it's stuck on 1 and I have no idea why. I am running the the php script of the localhost and its seems to be alright but the question is then its runs the php there is a possibility its don't work ? – Daniel Iliaguev Harta May 31 '13 at 11:49
  • By call I meant send : "ajax.send(null);" – Daniel Iliaguev Harta May 31 '13 at 11:58
  • It sounds like its hitting the server but not processing the code. Odd. So when you go to "http://localhost/yourfile.php?url=http://www.google.com" do you see the HTML code from google? – n00dle May 31 '13 at 12:45
  • Update : I think the problem maybe little diffident, ajax.readyState will always return 1 as long as its out the ajax.onreadystatechange function , However when the readyState is inside the ajax.onreadystatechange its returning undefined for some reason, in other words the if(ajax.readyState == 4) never works becouse its saying undefined == 4. right now I am trying to figure out why this is happening. – Daniel Iliaguev Harta May 31 '13 at 13:23
  • Ah, my bad, just spotted the problem - it's to do with the scope of the ajax variable and the fact that it's asynchronous, I'll test it and edit the code accordingly. – n00dle May 31 '13 at 14:12
  • Try it now - I've changed `var ajax` to `window.ajax`, which takes it from being a local variable (which is lost after the asynchronous call) to a global variable (which should be kept). – n00dle May 31 '13 at 14:15
  • I wish I could say its works but, I can't =\ for some reason It's returns undefined. I am looking right now if the problem is still appear to be still at the ready state thing or its something else now, WHY it's can't just work damn it. – Daniel Iliaguev Harta May 31 '13 at 14:30
  • after exploring seems that at bouth cases the "window.ajax.onreadystatechange" itself isn't working. I have no Idea why, I put inside just an return and it doesn't return very odd looks like don't activate maybe mean that there is no change in the ajax state. – Daniel Iliaguev Harta May 31 '13 at 14:38
  • I am one second away from committing a suicide, I took a example similar script from Microsoft examples just copied it and It's not working HOW the hell this can be ? What wrong with my script ? – Daniel Iliaguev Harta May 31 '13 at 15:25
  • It's not something blocking access to localhost is it? – n00dle May 31 '13 at 16:14
  • I don't sure, at the example I downloaded its working fine. but in my script its not. By the way this script is in a .js file if its make and diffidence. – Daniel Iliaguev Harta May 31 '13 at 16:37
  • What are the differences between the working one and the not-working one? Can you combine the relevant bits together to build something that works? – n00dle Jun 01 '13 at 05:45
  • you won't believe how stupid I am , that's what happened In win 8 app you cannot use alert statement so instead of the alert I put return the thing is that return in the on ready state is impossible and returns undefined and out side other this state men it's will always will return "1" because its returns the value as soon as the script loads . Few lessons for me in javascript , well now its just left the print somehow the result I don't know how because the function is in the js file and I have to use "list.push" to print, once again thank you very much for your help ! – Daniel Iliaguev Harta Jun 01 '13 at 08:17