944,056 Members | Top Members by Rank

Ad:
Mar 31st, 2005
0

What do you use to parse the url?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
willeffects is offline Offline
2 posts
since Mar 2005
Mar 31st, 2005
0

Re: What do you use to parse the url?

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 17th, 2007
0

Re: What do you use to parse the url?

Well, try this function, it should be working.
javascript Syntax (Toggle Plain Text)
  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. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Stan Vitvitskiy is offline Offline
1 posts
since May 2007
May 19th, 2007
0

Re: What do you use to parse the url?

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?
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Where to get good JavaScript Tutorials?
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: "add this site to your bookmarks" in firefox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC