1

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

Brad
  • 159,648
  • 54
  • 349
  • 530
user3440511
  • 29
  • 1
  • 1
  • 3
  • What do you mean by "fix url"? What do you mean by "url as the current page"? – Brad Mar 20 '14 at 05: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. – Damon Smith Mar 20 '14 at 05:06

8 Answers8

5

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;

tchow002
  • 1,068
  • 6
  • 8
  • 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? – user3440511 Mar 20 '14 at 17:09
  • Updated answer to get current URL without query string. – tchow002 Mar 20 '14 at 18:00
1

You can use

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

I hope you mean this

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

Amarnath R Shenoy
  • 5,121
  • 8
  • 24
  • 32
1

Use this code document.URL

Ex:

alert(document.URL);

You can get path split like

var split=document.URL.split("/");
Sajitha Rathnayake
  • 1,688
  • 3
  • 26
  • 47
-1

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

-1
var pathname = document.URL + 'Search=' + str;
AK47
  • 3,707
  • 3
  • 17
  • 36
-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;
Juan Garcia
  • 714
  • 6
  • 23
-1

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

window.location.search = "?searching="+str+"&Search";
rekire
  • 47,260
  • 30
  • 167
  • 264
-1

you can use window.location.pathname

Mumtaz Ahmad
  • 422
  • 5
  • 12
  • While this may be an answer to the question, you should also consider providing a short working example. – RedBaron Mar 20 '14 at 05:27