1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Simple Javascript Question: Changing URL on page

Discussion in 'HTML & JavaScript' started by NX_NULL, May 24, 2012.

Thread Status:
Not open for further replies.
  1. NX_NULL

    NX_NULL Regular Member Premium Member

    Joined:
    Dec 31, 2008
    Messages:
    309
    Likes Received:
    425
    I want to change the URL on my page when I receive traffic from mobile users.

    so when users from mobile device enter my page I want to change This link from :

    Code:
    <a class="checkout_link" title="Checkout" href="Desktop_checkout.php">Checkout</a>
    
    to
    Code:
    <a class="checkout_link" title="Checkout" href="Mobile_checkout.php">Checkout</a>
    
    I use this script for finding out mobile users

    Code:
    <script type="text/javascript">// <![CDATA[  
        var mobile = (/iphone|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));  
        if (mobile) {  
            document.location = "http://www.example.com";  
        }  
    // ]]></script>
    
    if its a mobile device it redirect to example.com, But i dont want to redirect I want to change URL on web page.



    rep and thanks will be given to any one help
    thanks
     
  2. szussino

    szussino Junior Member

    Joined:
    Mar 12, 2010
    Messages:
    109
    Likes Received:
    6
    What is different on the mobile checkout? I would detect this in the HTTP headers instead of Javascript.
     
  3. NX_NULL

    NX_NULL Regular Member Premium Member

    Joined:
    Dec 31, 2008
    Messages:
    309
    Likes Received:
    425
    I am not going to redirect users to different page, there is only one page and I can change links on that page
     
  4. The Doctor

    The Doctor Jr. VIP Jr. VIP

    Joined:
    Dec 18, 2010
    Messages:
    612
    Likes Received:
    188
    Occupation:
    Computer Scientist, Engineer, Programmer.
    Location:
    ☆☆☆☆☆☆
    no problem here you go :D

    Code:
    <script type="text/javascript">// <![CDATA[      var mobile = (/iphone|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));  
        if (mobile) {  
    		var checkoutLink = document.getElementById('checkout_link')
            checkoutLink.setAttribute("href","Mobile_checkout.php"); 
        }  
    // ]]></script>
    
    
    
    
    <a id="checkout_link" class="checkout_link" title="Checkout" href="Desktop_checkout.php">Checkout</a>
     
    • Thanks Thanks x 1
  5. NX_NULL

    NX_NULL Regular Member Premium Member

    Joined:
    Dec 31, 2008
    Messages:
    309
    Likes Received:
    425


    I tested it,

    in both ways viewing with Desktop or Mobile it shows only Desktop_checkout.php link
     
  6. The Doctor

    The Doctor Jr. VIP Jr. VIP

    Joined:
    Dec 18, 2010
    Messages:
    612
    Likes Received:
    188
    Occupation:
    Computer Scientist, Engineer, Programmer.
    Location:
    ☆☆☆☆☆☆
    oops. here make sure this is in the head...

    Code:
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
    you can put this wherever:

    Code:
    <script>$(document).ready(function()
    { 
        
        var mobile = (/iphone|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));  
        if (mobile) {  
            var checkoutLink = document.getElementById('checkout_link')
            checkoutLink.setAttribute("href","Mobile_checkout.php"); 
        }  
    });
    </script>
    and then this..

    Code:
    [LEFT][COLOR=#FFFFCC][I]<a id="checkout_link" class="checkout_link" title="Checkout" href="Desktop_checkout.php">Checkout</a>[/I][/COLOR][/LEFT]
     
    • Thanks Thanks x 1
  7. NX_NULL

    NX_NULL Regular Member Premium Member

    Joined:
    Dec 31, 2008
    Messages:
    309
    Likes Received:
    425
    thank you bro :)
     
  8. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    Issue solved, thread closed. Oh and no double posting in different section next time ;)
     
Thread Status:
Not open for further replies.