Jump to content

Combining 2 scripts


DBookatay

Recommended Posts

Hello,

 

I have a script that uses a pulldown menu to send the user to the selected page. I use this script to "filter" items on the page:


function faqRedirect(selectBox) {
	var faqURL = selectBox.options[selectBox.selectedIndex].value
		if (faqURL != "")
	{
		if (faqURL.substr(0, 4) != "http")
			faqURL = "../" + faqURL
		location.href = faqURL
	}
}

<select id="filter" name="filter" onChange="faqRedirect(this)">
<option value="search.php?cat=001&filter=yearDESC">Year: Newest First</option>
<option value="search.php?cat=001&filter=yearASC">Year: Oldest First</option>
<option value="search.php?cat=001&filter=priceDESC">Price: Highest First</option>
<option value="search.php?cat=001&filter=priceASC">Price: Lowest First</option>
<option value="search.php?cat=001&filter=milesASC">Mileage: Lowest First</option>
<option value="search.php?cat=001&filter=milesDESC">Mileage: Highest First</option>
</select>

 

 

Question is: how do I modify the current java section of the script to write a cookie to remember the users selection the next time the page loads?

 

Link to comment
Share on other sites

Use javascript, like the code below:

 

function faqRedirect(selectBox) {
	var faqURL = selectBox.options[selectBox.selectedIndex].value
		if (faqURL != "")
{
		document.cookie="SelectBoxPref="+faqURL.replace(/=/g,"#EQUALS#");
		if (faqURL.substr(0, 4) != "http")
			faqURL = "../" + faqURL
		location.href = faqURL
}
}

var cookies = document.cookie.split(";");
for (var i=0;i<cookies.length;i++) {
if (cookies[i].indexOf("SelectBoxPref=")==-1) continue;
var selectedUrl = cookies[i].split("=")[1].replace(/#EQUALS#/g,"=");
var selectOption = document.getElementById("filter").firstChild;
while (true) {
	if (selectOption.value==selectedUrl) {
		selectOption.selected = true;
		break;
	}
	if (!selectOption.nextSibling) break;
	selectOption = selectOption.nextSibling;
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.