[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

adding an additional link via jquery if the browser is safari

Posted on 2010-08-24
7
Medium Priority
?
459 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 83

Assisted Solution

by:leakim971
leakim971 earned 600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Accepted Solution

by:
SRigney earned 900 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 83

Expert Comment

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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…
Suggested Courses

873 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