I have a jQuery menu bar, but a problem with external links. I kept getting the Error:
Syntax error, unrecognized expression: http://google.com
after a post here i changed the selector from var item = $($(this).attr("href"));
to var item = $(this).attr("href");
and the external links worked.
i went on and added more functionality but after that the same error kept poping up.
any thoughts?
Here is my code:
<ul id="menu">
<li><a href="#home">HOME</a></li>
<li><a href="#about-us">ABOUT US</a></li>
<li><a href="http://www.google.com" class="external">EXTERNAL</a></li>
<li><a href="#contact">CONTACT</a></li>
</ul>
And the js:
var lastId,
topMenu = $("#menu"),
topMenuHeight = topMenu.outerHeight()+145,
menuItems = topMenu.find("a"),
scrollItems = menuItems.map(function(){
var item = $($(this).attr("href"));
if (item.length) { return item; }
});
$('a.external').click(function() {
this.target = "_blank";
});
$('a[href*=#]').bind('click', function(e) {
e.preventDefault();
var target = $(this).attr("href");
$('html, body').stop().animate({
scrollTop: $(target).offset().top
}, 3000, function() {
});
return false;
});
$(window).scroll(function(){
var fromTop = $(this).scrollTop()+topMenuHeight;
var cur = scrollItems.map(function(){
if ($(this).offset().top < fromTop)
return this;
});
cur = cur[cur.length-1];
var id = cur && cur.length ? cur[0].id : "";
if (lastId !== id) {
lastId = id;
menuItems
.parent().removeClass("active")
.end().filter("[href=#"+id+"]").parent().addClass("active");
}
});
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
$(".footer").css( "position", "relative" );
$(".contact").css( "marginBottom", "0" );
}
else
{
// FadeTo elements
if ( $(window).width() > 1023) {
tiles = $("h2, h3, .grid li, .contact .content .form, .contact .content .contact-text ").fadeTo(0, 0);
$(window).scroll(function(d,h) {
tiles.each(function(i) {
a = $(this).offset().top + $(this).height();
b = $(window).scrollTop() + $(window).height();
if (a < b) $(this).fadeTo(1000,1);
});
});
}
}
$('a.external').click(function() {
var target = $(this).attr("href");
this.target = "_blank";
return false;
});
//Menu mobile click
$( ".icon" ).click(function() {
$( " ul.menu-click" ).slideToggle( "slow", function() {
// Animation complete.
});
});
$(window).load(function(){
$(".preloader").delay(1000).fadeOut("slow")
// Parallax
if ($('.parallax-background').length) {
$(".parallax-background").parallax();
}
// Parallax
if ($('.parallax-background-partners').length) {
$(".parallax-background-partners").parallax();
}
});