3

I have this portion of the code. I want to know how can I read the url as the current page instead a fix url

Here is the portion of the coding:

var str='';
      if(model_id != 0 ) str = model_id+"+";
      if(year_id != 0 ) str +=year_id+"+"; 
      url='http://store.ijdmtoy.com/SearchResults.asp?Search='+str;
top.location.href=url;

You see currently it has a fix reading the url http://store.ijdmtoy.com/SearchResults.asp

For example, I am currently on http://store.ijdmtoy.com/abs.htm

How can I change the code so it will automatically read as http://store.ijdmtoy.com/abs.htm?searching=Y&Search

2
  • What do you mean by "fix url"? What do you mean by "url as the current page"? Commented Mar 20, 2014 at 5:02
  • It sounds like what you want to do is get the whole url except for the search param so that you don't have to hard code the url. You can get the parts of the current url with window.location.hostname, .pathname, .search and .hash. You can construct your url again using those, or just get the whole window.location.href and split it on the equals sign. Commented Mar 20, 2014 at 5:06

8 Answers 8

6

If you are currently on http://store.ijdmtoy.com/abs.htm then document.URL will be http://store.ijdmtoy.com/abs.htm.

You can try:

url = location.protocol + '//' + location.host + location.pathname + '?Search=' + str;

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Tchow002, this works! However after the first search the current URL will become for example like store.ijdmtoy.com/abs.htm?searching=Y&Search=Integra+1999. Then if I am to do a second second search this way, it won't return correctly. So is there any way I can set it to only read the current URL and end it at after the .htm?
Updated answer to get current URL without query string.
1

You can use

      document.URL  or
      window.location.href.toString()

I hope you mean this

http://jsfiddle.net/AmarnathRShenoy/q3yCA/

Comments

1

Use this code document.URL

Ex:

alert(document.URL);

You can get path split like

var split=document.URL.split("/");

Comments

-1

rather than using top.location.href=url; you can use window.location = url;

Comments

-1
var pathname = document.URL + 'Search=' + str;

Comments

-1

Get it from window.location, as it is an object "cast it" to an string:

var global = (function(){ return this; })() ;
var str='';
      if(model_id != 0 ) str = model_id+"+";
      if(year_id != 0 ) str +=year_id+"+"; 
      url= '' + global.location + '?Search='+str;
top.location.href=url;

Comments

-1

If you just want to replace the query string use this:

window.location.search = "?searching="+str+"&Search";

Comments

-1

you can use window.location.pathname

1 Comment

While this may be an answer to the question, you should also consider providing a short working example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.