ndek
asked on
adding an additional link via jquery if the browser is safari
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-
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-
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
$(document).ready(function
jQuery.each(jQuery.browser
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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>");
}
});
$(document).ready(function
if ($.browser.safari) {
$("#Mobile #MainMenu ul").append("<li><a href=\"#\">iPhone App</a></li>");
}
});
ASKER
Opps realized only two commented, but thanks to you both.
Thanks for the points!
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.