hello,
the title basically sums it up. how can i see if a word is in a url and then if it is, do something with it.

thanks for any help
billy

Well, you could save the url you'd like to search into a string and use the JavaScript match() method: stringObject.match(searchvalue) to search for a specific word inside the string. If you want to, you can also opt for a different method and use Regular Expressions to search for recurring patterns within a string.

You can usually find information like this at W3Schools.

Good luck! :)

i would say php or html forms
you can test to see if something equals some thing i would say php would be a good bet but it depends on what you are trying to do if you provide a little more info i can help alot more


for examples

www.yousite.com/somepage.php?x=itself&y=betterbegood

or you can post the data and not see it in the url but still be able to
pass it from page to page and test it to do some thing with it

The best way to do this is to use Regular Expressions (RegExp). This is a useful, shorthand way to search a string for a certain value.

Here's a regular expression to search for a word in JavaScript. Regular Expressions work in most programming languages, so if you're using another, say so and I'll change it.

var word = "Dani";
var url = document.location.href;
var regExp = new RegExp(word, 'i');

if (regExp.test(url))
	alert("The word " + word + " is in the URL!");
else
	alert("The word " + word + " is NOT in the URL.");

Change the variable word to whatever word you need to search for.

Billymcguffin,

Or are you trying to parse out a particular name=value pair from a querystring? ie. the part of the URL to the right of the ? .

Airshow

thanks for all the feedback.
what im trying to do is actually have a link that leads to a page, and when the page loads, it does a function. but i cant use onload because i want it to do the function only when you click the specific link.

i figured that if i could include a specific keyword in the href part of the link, then i could test for it, and if it was there, it would do the function. its probably not possible, though, so is there another way to do this?

sorry about the change (kinda) in topic.

help is appreciated!

This is completely possible, although it would be more useful to do this server-side (PHP) rather than client-side (JavaScript).

Here is an example of how to do it in JavaScript anyway, if you have limited resources. This assumes that your URL uses a variable called "run", which causes the script to load if run=1. http://www.example.com/index.php?run=1&foo=bar

var regExp = /(\?|&)run=./i;

if (regExp.test(document.location.href)) {
	var run = regExp.exec(document.location.href)[0];
	var value = /.$/.exec(run)[0];
	if (value == 1)
		myFunction();
}

would it be possible to have the keyword in the url be passed into the function so that a different keyword does a different function?

Sure. Just make another if statement.

var url = document.location.href;
var queryVar = "run";
var regExp = new RegExp("(\\?|&)" + queryVar + "=[\\w\\d]*", "i");

if (regExp.test(url)) {
	var query = regExp.exec(url)[0];
	var value = query.substr(queryVar.length + 2);

	if (value == "myFunction")
		myFunction();
	else if (value == "otherFunc")
		otherFunc();
}

Change queryVar to another word if you want to change the ?run= to anything else. For example, if you change it to load , it makes the URL search for ?load= .

The variable value is a string that is the value of ?run=VALUE HERE . Add a new if statement to run a new function.

* note: When modifying my script, make sure you don't make it run whatever function is in the URL. This could open some dangerous security holes that would allow anyone to run any JavaScript call.

awesome. so can i just stick this in the on load, or could i just have it there without sitcking it in onload.

2 more things. do i have to include the &foo=bar part? and how do i say this thread is solved?

  1. The script can go into your page as-is without waiting for the page load. So you could just insert it into the <body> or <head>.
  2. No, you don't have to include &foo=bar at all. That was just to show that you could have other variables without it affecting the script.
  3. I don't know how to mark this thread as solved, sorry.

i am having some problems.

here is my modified version of your script, as well as the corresponding html:

<head>
<script type="text/javascript">
var url = document.location.href;
var queryVar = "run";
var regExp = new RegExp("(\\?|&)" + queryVar + "=[\\w\\d]*", "i");

if (regExp.test(url)) {
     var query = regExp.exec(url)[0];
     var value = query.substr(queryVar.length + 2);

     if (value == 'div2')
          switchPic("div2");
     else if (value == 'div3')
          switchPic("div3");
}

function switchPic(n) {
document.getElementById(n).style.display="block";
}
</script>
</head>
<body>

<div class="picture" id="div2">HI</div>

<a href="http://mysite.com/index.shtml?run='div2'">Click Here</a>

can you spot anything wrong with this code?

sorry about this, but i have very little experience with javascript.
thanks for all your help.

I found a few things wrong. First, since you put my code into a function, the function has to be called before it runs. This means that you have to add it to the onload attribute.

If you take the script out of the function check(), you don't have to call it with onLoad. But if you still need the function for later during runtime, you can leave it as is.

Also, when you make the url, you don't have to include quotes around 'div2'. You can leave it as ?run=div2 and it will work.

Here is a cleaned up version of your script:

<html>
<head>
<script type="text/javascript">
var url = document.location.href;
var queryVar = "run";
var regExp = new RegExp("(\\?|&)" + queryVar + "=[\\w\\d]*", "i");

function check() {
	if (regExp.test(url)) {
		var query = regExp.exec(url)[0];
		var value = query.substr(queryVar.length + 2);

		if (value == "div2")
			switchPic("div2");
		else if (value == "div3")
			switchPic("div3");
	}
	function switchPic(n) {
		document.getElementById(n).style.display="block";
	}
}
</script>
</head>
<body onLoad="check()">

<div class="picture" id="div2" style="display:none;">HI</div>

<a href="test_new.html?run=div2">Click Here</a>

</body>
</html>

Do you want to use the switchPic(n) function other times during runtime? You might want to take it outside of the check() function and make it a global function, like so:

var url = document.location.href;
var queryVar = "run";
var regExp = new RegExp("(\\?|&)" + queryVar + "=[\\w\\d]*", "i");

function check() {
	if (regExp.test(url)) {
		var query = regExp.exec(url)[0];
		var value = query.substr(queryVar.length + 2);

		if (value == "div2")
			switchPic("div2");
		else if (value == "div3")
			switchPic("div3");
	}
}
function switchPic(n) {
	document.getElementById(n).style.display="block";
}
This question has already been answered. Start a new discussion instead.