javascript - Defining selected tab by url in jQuery tab script -
javascript - Defining selected tab by url in jQuery tab script -
i have jquery search script uses tabs user define search type want use. when user searches, url created #type/query/. if #type in url tab corresponds type appears selected. however, if query in url (#type/query/) tab isn't selected. code below, how can create ignore after /query/? hope can understand i'm trying describe.
my jquery script is:
$(document).ready(function () { $('[id^=type_]').click(function () { type = this.id.replace('type_', ''); $('[id^=type_]').removeclass('selected'); $('#type_' + type).addclass('selected'); homecoming false; }); if (window.location.hash != "") { url = window.location.hash.replace('#', ''); $('#type_' + url).click(); } else { $('#type_search').click(); } $('#query').keyup(function () { var query = $(this).val(); var url = '/' + type + '/' + query + '/'; window.location.hash = '' + type + '/' + query + '/'; document.title = $(this).val() + ' - search'; $('#results').show(); if (query == '') { window.location.hash = ''; document.title = 'my search'; $('#results').hide(); } $.ajax({ type: 'get', url: url, datatype: 'html', success: function (results) { $('#results').html(results); } }); }); var textlength = $('#query').val().length; if (textlength <= 0) { $('#query').focus(); } else { $('#query').blur(); } });
the clumsy way came is:
$('a').each( function(){ var url = this.href; var hashat = url.indexof('#'); var nextslashat = url.indexof('/',hashat); var url = url.substring(0,nextslashat); var tab = url.substring(hashat); $(this).text(url + " (" + tab + ")."); });
js fiddle demo.
obviously rather iterating through a
elements, you'll want assign url
variable window.location;
, on should adapted needs, think.
references:
indexof()
. substring()
. javascript jquery html
Comments
Post a Comment