From ae27ad890c791143f513b5161fabd843e8ad3c92 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 15 Mar 2010 13:48:07 +0200 Subject: [PATCH] improved redirect_to method and attached it to the window object --- suggest_results/jquery.suggest_results.js | 39 +++++++++++------------ 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/suggest_results/jquery.suggest_results.js b/suggest_results/jquery.suggest_results.js index bfdcf8d..99ad2ae 100644 --- a/suggest_results/jquery.suggest_results.js +++ b/suggest_results/jquery.suggest_results.js @@ -128,7 +128,7 @@ }; self.list.html(html); $(".result", self.list).click(function(){ - self.redirect_to($("a", $(this)).attr("href")); + window.redirect_to($("a", $(this)).attr("href")); }).hover(function(){ $(".selected", self.list).removeClass("selected"); $(this).addClass("selected"); @@ -225,7 +225,7 @@ $.fn.suggest_results.activate_selected = function(options){ var self = $.fn.suggest_results; if (self.selected_result !== null) { - self.redirect_to(self.current_results[self.selected_result].href); + window.redirect_to(self.current_results[self.selected_result].href); }; }; @@ -305,24 +305,6 @@ return string; }; - $.fn.suggest_results.redirect_to = function(url, location){ - if (typeof(location) == "undefined") { - location = window.location; - }; - var redirect_to = ""; - if (url.match(/.+\:\/\/.+/) === null) { - redirect_to += location.protocol + "//"; - redirect_to += location.hostname; - if (location.port != "") { redirect_to += ":" + location.port; }; - if (url.charAt(0) !== "/") { - redirect_to += location.pathname.substr(0, location.pathname.lastIndexOf("/")+1); - }; - window.location.href = redirect_to + url; - } else { - window.location.href = url; - }; - }; - $.fn.suggest_results.setTimeout = function(callback, delay){ var self = $.fn.suggest_results; self.clearTimeout(); @@ -375,6 +357,23 @@ })(jQuery); +/* + redirect_to method from: http://gist.github.com/327227 +*/ +window.redirect_to = function(url, location){ + var redirect_to = ""; + if (typeof(location) == "undefined") location = window.location; + if (url.match(/^[a-zA-Z]+\:\/\/.+/) === null) { + redirect_to += location.protocol + "//" + location.hostname; + if (location.port != "") redirect_to += ":" + location.port; + if (url.charAt(0) !== "/") redirect_to += location.pathname.substr(0, location.pathname.lastIndexOf("/")+1); + window.location.href = redirect_to + url; + } else { + window.location.href = url; + }; +}; + + /* Crossbrowser hasOwnProperty solution, based on answers from: http://stackoverflow.com/questions/135448/how-do-i-check-to-see-if-an-object-has-an-attribute-in-javascript