The $.Param( ) Inverse Function in JavaScript/Jquery

The $.param( ) inverse function in JavaScript / jQuery

You should use jQuery BBQ's deparam function. It's well-tested and documented.

new to jquery/javascript i was try to find an inverse to this function

You're putting the :not at the wrong place, it's part of the selector query not JS code

jQuery(document).ready(function() {
$("#rad1 :checkbox").click(function() {

$("div.abc1").show();
$("#rad1 :checkbox:not(:checked)").each(function() {
$("." + $(this).val()).hide();
});
});
});

In Javascript, why write var QueryStringToHash = function QueryStringToHash (query) { ... }?

Declaring a function means that it's defined when the script block is parsed, while assigning it to a variable is done at runtime:

x(); // this works as the function is defined before the script block is executed

function x() {}

but:

x(); // doesn't work as x is not assigned yet

var x = function() {}

Assigning a function to a variable can be done conditionally. Example:

var getColor;
if (color == 'red') {
getColor = function() { return "Red"; }
} else {
getColor = function() { return "Blue"; }
}

How to jump to a particular page's some drop-down's particular value using JavaScript?

You could combine two things: URI Anchors (adding # in your uri) and GET parameters (? followed by values) to make something like example.com#first-drop?firstDropDown=nj

In JavaScript (on page2.html) all you'd have to do would be this:

//Run the script on page load.window.onload = function() {  var url = window.location.href;
//Let me cheat since we don't have page1 and page2 in this example... url += "#firstDropDown?firstDropDown=ny";
// Select the text after the # and before the ? symbol. var dropDown = url.substring(url.indexOf('#') + 1, url.indexOf('?') - 1); // Select the text after the ? symbol var selectedOption = url.substring(url.indexOf('?') + 1);
// Filter the dropdown's ID and its value into an object. selectedOption = { elem: selectedOption.substring(0, selectedOption.indexOf('=')), value: selectedOption.substring(selectedOption.indexOf('=') + 1) };
// Get the dropdown DOMElement in the page so we can change the selectedIndex. dropDown = document.querySelector('#' + selectedOption.elem); var index = 0, selectedIndex = 0;
// Loop through dropDown's children element to find the right one. dropDown.childNodes.forEach(function(elem, i) { // Make sur to look for DOMElement nodes only. if (elem.nodeType == 1) {
if (elem.value === selectedOption.value) { elem.setAttribute("selected", "selected"); selectedIndex = index; return true; } index++; } }); // Now that we have the right index, let's make sure it's selected. dropDown.selectedIndex = selectedIndex;}
<select id="firstDropDown">  <option value="nj">New Jersey</option>  <option value="ny">New York</option>  <option value="la">Los Angeles</option></select>

How to modify URL query string with Jquery

Regular Expression is extremely powerful and perfect for these sorts of situations, but difficult to learn. This is how to use it in your example.

$("#featured-url").attr('href',
$("#featured-url").attr('href').replace(/\?tab=[^&]+/, '?tab=featured')
);

$("#school-url").attr('href',
$("#school-url").attr('href').replace(/\?tab=[^&]+/, '?tab=all_schools')
);

Here's an explanation of the RegEx:

  • \? matches the ? character (escaped, since ? has other meanings)
  • tab= matches that string of characters
  • [^&] matches anything except an &
  • + expects there to be one or more of the previous match

Inverse operation for Javascript's Function.toString()

You could use the Function constructor to avoid creating a global variable:

var func = Function("return " + so.actions[i].func)();

This is equivalent to:

var func = eval("(function () { return " + so.actions[i].func + " })()");

One thing to be wary of is the surrounding closure which you won't be able to capture, so variables inside the function will "change" when you serialize and de-serialize.

edit

I'm not thinking, you can of course just do:

var func = eval("(" + so.actions[i].func + ")");


Related Topics



Leave a reply



Submit