javascript - How to turn function into jQuery plugin -
here's jsfiddle: http://jsfiddle.net/jsfiddleplayer/vhg7csb7/10/
i trying implement answer in stackoverflow question jquery plugin , having no luck. getparameterbyname function reads url's querystring variables , returns 1 indicated in argument. however, code keeps saying:
.getparameterbyname not function
my jquery code follows:
$(document).ready(function(){ var paramabc = $.getparameterbyname('abc'); alert(paramabc); }); (function ($) { /* gets value of querystring variable name */ $.fn.getparameterbyname = function (name) { name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); var regex = new regexp("[\\?&]" + name + "=([^&#]*)"), results = regex.exec(('http://www.myexample.com/?abc=1&def=2').search); return results === null ? "" : decodeuricomponent(results[1].replace(/\+/g, " ")); }; }(jquery)); note i've hardcoded location in above function because jsfiddle doesn't have querystring variables on url. original code line was:
results = regex.exec(location.search); if code working, return 1 (because abc=1 in querystring) , display in alert(). doing wrong? thanks.
you there use $ instead of $.fn, $.fn alias prototype of jquery , expects selector
$.getparameterbyname = function (name) { // }; note: define plugin code first use it.
Comments
Post a Comment