0

how do i write a function in javascript that can get the current url eg:

http://www.blahblah.com/apps/category.php?pg=1&catId=3021

and depending on a user selection choice, appends another parameter to the url like:

http://localhost/buyamonline/apps/category.php?pg=1&catId=3021&limit=5

but heres the catch:

Everytime the user selects a diff choice, I dont want to keep appending stuff like

http://localhost/buyamonline/apps/category.php?pg=1&catId=3021&limit=5&limit=10 and so on.

I want to always replace add it if there is no limit parameter or replace the value if there is one.

I was trying to accomplish this using sprintf but failed.

i was doing like so:

var w = document.mylimit.limiter.selectedIndex;
var url_add = document.mylimit.limiter.options[w].value;
var loc = window.location.href;
window.location.href = sprintf(loc+"%s="+%s, "&limit", url_add);
2
  • There is no sprintf in Javascript unless you write one. Commented Jan 31, 2010 at 8:21
  • i got a sprintf wrapper written by some dude webtoolkit.info/javascript-sprintf.html - since you understand my question, do you have any solution? thnx Commented Jan 31, 2010 at 8:27

2 Answers 2

1

Updated with regular expression solution implemented in JS.

  1. You can use a regular expression using JavaScript replace() Method
  2. You can use build the entire url each time rather than just appending a parameter
  3. You can parse the url into it's parts before appending the parameter

Which would you prefer?

For #1: The following should do the trick. Note though, there are issues with using regular expressions for parsing urls. (Ref: stackoverflow.com/questions/1842681/…)

<script type="text/javascript">
  var pattern = "&limit(\=[^&]*)?(?=&|$)|^foo(\=[^&]*)?(&|$)";
  var modifiers = "";
  var txt=new RegExp(pattern,modifiers);
  var str="http://localhost/buyamonline/apps/category.php?pg=1&catId=3021&limit=5";
  document.write(str+"<br/>");
  var replacement = "&limit=10";
  document.write(str.replace(txt, replacement));
</script>
1
  • regex with javascript replace method i think should do. Commented Jan 31, 2010 at 8:32
1

Below you can find my sprintf implementation which you can use it in your JS code to achieve what you need. It works almost like the C++/Java/PHP sprintf function with some limitations: the format specifiers are written like %1 and does not support the typed format specifiers (like %d, %s, %.2f, etc).

String.prototype.sprintf = function() {
  var matches,result = this, p = /%(\d)/g;
  while (matches = p.exec(result)) {
    result = result.replace(matches[0], arguments[parseInt(matches[1]) - 1]);
  }

  return result;
};

Syntax:

format.sprintf(arg1,arg2,...);

The format string is composed of zero or more format specifiers that follows this prototype:

  • a % followed by argument index, where the first argument has index 1.

The arg1, arg2, ... are the variable strings which will replace the format specifiers.

Example: 'The quick %1 fox jumps over the %2 dog'.sprintf('brown','lazy');

Usage example:

var str = 'The size of %1 (%2) exceeds the %3 (%4).';
console.log(str.sprintf('myfile.txt', '100MB', 'max. allowed size', '75MB'));

Output:

The size of myfile.txt (100MB) exceeds the max. allowed size (75MB).

Note: If you need a robust sprintf function then check the sprintf for JavaScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.