Solved

adding an additional link via jquery if the browser is safari

Posted on 2010-08-24
7
448 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
The viewer will learn how to dynamically set the form action using jQuery.
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now