Solved

adding an additional link via jquery if the browser is safari

Posted on 2010-08-24
7
453 Views
Last Modified: 2013-12-07
I'm creating a basic mobile template, and would like to add an additional link to my MainNav if the browser is safari (to link to an iPhone specific app) but not have the link appear on any other devices.

Below is the MainNav and I would like  <li><a href="#">iPhone App</a></li> to be inserted after the Menu Item 3 li.

<div id="MainMenu">
  <ul>
   <li><a href="#">Menu Item 1</a></li>
   <li><a href="#">Menu Item 2</a></li>
   <li><a href="#">Menu Item 3</a></li>
  </ul>
</div>

Is this possible to do with jquery to detect the browser and then insert the link via last child? Or is there a better way to accomplish this?

Thanks in advance-
0
Comment
Question by:ndek
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Expert Comment

by:SRigney
ID: 33511862
http://api.jquery.com/jQuery.browser/#jQuery.browser1

jQuery has a .browser function that will help you with that.

I'd add a $(document).ready();
method and if it is safari insert the link where you want it.

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 200 total points
ID: 33517315
Try :


$(document).ready(function() {
     if($.browser.webkit) {
        $("ul", "#MainMenu").append("<li><a href=\"#\">iPhone App</a></li>");
     }
});

Open in new window

0
 

Author Comment

by:ndek
ID: 33533380
Sorry for the delay, I got pulled off this project for a bit, but currently have

$(document).ready(function() {
   jQuery.each(jQuery.browser, function(i) {
        if ($.browser.safari) {
            $("#Mobile #MainMenu ul").append("<li><a href=\"#\">iPhone App</a></li>");
        }
    });
});

It is working, unfortunately it is adding the iPhone li 3 times.  I tested in mozilla and it repeats it just twice.  jQuery recommends using .support, but honestly I think the .browser option is the best for this, since they want certain links made viewable to iphone users.

In Safari I view the code and the injected html isn't appearing in the source.

Any ideas on why it is repeating?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Accepted Solution

by:
SRigney earned 300 total points
ID: 33534289
it's probably repeating because you are looping through each browser.  I thin you just need the if statement without the jQuery.each loop.
0
 

Author Comment

by:ndek
ID: 33534907
Removing the jQuery.each stopped the repeating thanks to all three of you.


$(document).ready(function() {
        if ($.browser.safari) {
            $("#Mobile #MainMenu ul").append("<li><a href=\"#\">iPhone App</a></li>");
        }
});
0
 

Author Closing Comment

by:ndek
ID: 33534925
Opps realized only two commented, but thanks to you both.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33535506
Thanks for the points!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
This article discusses how to implement server side field validation and display customized error messages to the client.
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
Shows how to create a shortcut to site-search Experts Exchange using Google in the Chrome browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch the Search Engine Menu: In chrome, via you…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question