What do you use to parse the url?

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Mar 2005
Posts: 2
Reputation: willeffects is an unknown quantity at this point 
Solved Threads: 0
willeffects willeffects is offline Offline
Newbie Poster

What do you use to parse the url?

 
0
  #1
Mar 31st, 2005
I wouldn't even consider myself a rookie with javascript but know it can be done, so thanks for any help.

What function would I use to have javascript read the page it is on, grab the url which is like:
http://site.com/boards/index.php?sho...37&mode=linear

and say if the url includes "mode=linear" then apply this style sheet?

Thanks!
Will
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: What do you use to parse the url?

 
0
  #2
Mar 31st, 2005
I suppose you could do a
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. somevariable = window.location;

and then do a substr or substring on the url, and see if it's in there. There might be an easier way, but that's probably how I'd do it.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1
Reputation: Stan Vitvitskiy is an unknown quantity at this point 
Solved Threads: 0
Stan Vitvitskiy Stan Vitvitskiy is offline Offline
Newbie Poster

Re: What do you use to parse the url?

 
0
  #3
May 17th, 2007
Well, try this function, it should be working.
  1. function parseURL(buffer) {
  2. var result = { };
  3. result.protocol = "";
  4. result.user = "";
  5. result.password = "";
  6. result.host = "";
  7. result.port = "";
  8. result.path = "";
  9. result.query = "";
  10.  
  11. var section = "PROTOCOL";
  12. var start = 0;
  13. var wasSlash = false;
  14.  
  15. while(start < buffer.length) {
  16. if(section == "PROTOCOL") {
  17. if(buffer.charAt(start) == ':') {
  18. section = "AFTER_PROTOCOL";
  19. start++;
  20. } else if(buffer.charAt(start) == '/' && result.protocol.length() == 0) {
  21. section = PATH;
  22. } else {
  23. result.protocol += buffer.charAt(start++);
  24. }
  25. } else if(section == "AFTER_PROTOCOL") {
  26. if(buffer.charAt(start) == '/') {
  27. if(!wasSlash) {
  28. wasSlash = true;
  29. } else {
  30. wasSlash = false;
  31. section = "USER";
  32. }
  33. start ++;
  34. } else {
  35. throw new ParseException("Protocol shell be separated with 2 slashes");
  36. }
  37. } else if(section == "USER") {
  38. if(buffer.charAt(start) == '/') {
  39. result.host = result.user;
  40. result.user = "";
  41. section = "PATH";
  42. } else if(buffer.charAt(start) == '?') {
  43. result.host = result.user;
  44. result.user = "";
  45. section = "QUERY";
  46. start++;
  47. } else if(buffer.charAt(start) == ':') {
  48. section = "PASSWORD";
  49. start++;
  50. } else if(buffer.charAt(start) == '@') {
  51. section = "HOST";
  52. start++;
  53. } else {
  54. result.user += buffer.charAt(start++);
  55. }
  56. } else if(section == "PASSWORD") {
  57. if(buffer.charAt(start) == '/') {
  58. result.host = result.user;
  59. result.port = result.password;
  60. result.user = "";
  61. result.password = "";
  62. section = "PATH";
  63. } else if(buffer.charAt(start) == '?') {
  64. result.host = result.user;
  65. result.port = result.password;
  66. result.user = "";
  67. result.password = "";
  68. section = "QUERY";
  69. start ++;
  70. } else if(buffer.charAt(start) == '@') {
  71. section = "HOST";
  72. start++;
  73. } else {
  74. result.password += buffer.charAt(start++);
  75. }
  76. } else if(section == "HOST") {
  77. if(buffer.charAt(start) == '/') {
  78. section = "PATH";
  79. } else if(buffer.charAt(start) == ':') {
  80. section = "PORT";
  81. start++;
  82. } else if(buffer.charAt(start) == '?') {
  83. section = "QUERY";
  84. start++;
  85. } else {
  86. result.host += buffer.charAt(start++);
  87. }
  88. } else if(section == "PORT") {
  89. if(buffer.charAt(start) = '/') {
  90. section = "PATH";
  91. } else if(buffer.charAt(start) == '?') {
  92. section = "QUERY";
  93. start++;
  94. } else {
  95. result.port += buffer.charAt(start++);
  96. }
  97. } else if(section == "PATH") {
  98. if(buffer.charAt(start) == '?') {
  99. section = "QUERY";
  100. start ++;
  101. } else {
  102. result.path += buffer.charAt(start++);
  103. }
  104. } else if(section == "QUERY") {
  105. result.query += buffer.charAt(start++);
  106. }
  107. }
  108.  
  109. if(section == "PROTOCOL") {
  110. result.host = result.protocol;
  111. result.protocol = "http";
  112. } else if(section == "AFTER_PROTOCOL") {
  113. throw new ParseException("Invalid url");
  114. } else if(section == "USER") {
  115. result.host = result.user;
  116. result.user = "";
  117. } else if(section == "PASSWORD") {
  118. result.host = result.user;
  119. result.port = result.password;
  120. result.user = "";
  121. result.password = "";
  122. }
  123.  
  124. return result;
  125. }
  126.  
  127. function ParseException(description) {
  128. this.description = description;
  129. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,203
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 164
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: What do you use to parse the url?

 
0
  #4
May 19th, 2007
I don't unserstand why the Javascript needs to know the url of the calling page. Doesn't the person who wrote both of them already know that?
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC