Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to other link if a web page is not available in HTML/JavaScript

Is there any option in HTML or JavaScript for when a user clicks on a link, if that link is available then done, if not then goes on another link.

Here is part of my code:

<div id="sidebar-collapse" class="col-sm-3 col-lg-2 sidebar">
 <ul>
    <li><a href="/returns"><span class="glyphicon glyphicon-log-in"></span> Returns</a></li>
    <li  class="active"><a href="/pay_reco" target="_blank"><span class="glyphicon glyphicon-log-in"></span> Payment Recouncillation </a></li>
    <li ><a href="/pay_get" target="_blank"><span class="glyphicon glyphicon-log-in"></span> Payment Get </a></li>
    <li><a href="/import"><span class="glyphicon glyphicon-log-in"></span> Import </a></li>
    <li>

<!-- Here I want to add  Two link in a href tag if first link does not 

available (web page not available) then it go to another link
        <!-- <object data="http:/127.0.0.1:9004" type="href" > -->
        <a target="_blank" href="http://127.0.0.1:9001">
            <span class="glyphicon glyphicon-log-in"></span>
             ComPare
        </a>
    </li>
    <li><a target="_blank" href="http://127.0.0.1:9000"><span class="glyphicon glyphicon-log-in"></span> Portal </a></li>

like image 487
Shubham Batra Avatar asked Nov 21 '15 06:11

Shubham Batra


People also ask

How do I link to another page in JavaScript?

Answer: Use the JavaScript window. location Property If you want to redirect the user from one page to another automatically, you can use the syntax window. location. replace("page_url") .

How do I redirect a previous page in HTML?

history.go() method to redirect the browser into previous page.

Can JavaScript read the source of any web page?

If you absolutely need to use javascript, you could load the page source with an ajax request. Note that with javascript, you can only retrieve pages that are located under the same domain with the requesting page.

How do you go to a page in JavaScript?

There are three ways to go to a URL in JavaScript: using window. location, the window. location. assign() method, or the window.


1 Answers

Step 1: Update your HTML code like below.

<a id="selector" href="javascript:void(0)">
            <span class="glyphicon glyphicon-log-in"></span>
             ComPare
</a>

Step 2: After update your HTML, handle click event with jQuery and send ajax request, if response ok, go link 1, or another case you route to user another url.

$(function(){
    $('body').on('click', '#selector', function() {
        $.ajax({
            type: "GET",
            url: "http://127.0.0.1:9001",    
            complete: function(xhr, textStatus) {
               if(xhr.status == 200) window.location = "http://127.0.0.1:9001";
               else window.location = "http://127.0.0.1:9004";
            } 
        });
    })
})

I created little jQuery plugin for this case on here

With AnotherJS:

HTML:

<a href="http://127.0.0.1:9001" data-another="http://127.0.0.1:9004">Link Text</a>

Javascript:

$(function() {
   $('a').another();
})

That's all! :)

like image 139
Furkan Başaran Avatar answered Sep 28 '22 07:09

Furkan Başaran