User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 375,249 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,147 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 9270 | Replies: 3
Reply
Join Date: Mar 2005
Posts: 2
Reputation: willeffects is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
willeffects willeffects is offline Offline
Newbie Poster

What do you use to parse the url?

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2004
Location: Lincoln Park, Michigan
Posts: 1,744
Reputation: Comatose is an unknown quantity at this point 
Rep Power: 7
Solved Threads: 107
Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Moderator

Re: What do you use to parse the url?

  #2  
Mar 31st, 2005
I suppose you could do a
     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  
Join Date: May 2007
Posts: 1
Reputation: Stan Vitvitskiy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Stan Vitvitskiy Stan Vitvitskiy is offline Offline
Newbie Poster

Re: What do you use to parse the url?

  #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  
Join Date: Jan 2007
Posts: 2,433
Reputation: MidiMagic is on a distinguished road 
Rep Power: 6
Solved Threads: 99
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Posting Maven

Re: What do you use to parse the url?

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb JavaScript / DHTML / AJAX Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 4:52 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC