Javascript based query parser for AJAX application

CK CK 3788 
Created at
Updated at  
567 0 0

When you want to pass data from one web page to another when server-side scripting is not available, and without setting a cookie? Here's the really good javascript you can use.

<script>
function QueryParser(str)
{
  if (str)
  {
    str = unescape(str);
    if (str.indexOf("?") === 0)
    {
      str = str.substring(1);
    }
    var args = str.split("&");
    for (var i = 0; i < args.length; i++)
    {
      var pair = args[i].split("=");
      if (pair.length >= 1)
      {
        var prop = pair.shift();
        this[prop] = (pair.length == 1) ? pair[0] : (pair.length > 1) ? pair.join('=') : '';
      }
    }
  }
  this.set = function (prop, value) { return this[prop] = value; };
  this.clear = function (prop)
  {
    if(typeof this[prop] !== 'undefined')
    {
      this.set(prop, null);
      return true;
    }
    else { return false; }
  };
  this.build = function (baseURL, hashName)
  {
    baseURL = (!baseURL || typeof baseURL !== 'string') ? '?' : (baseURL.indexOf("?") === -1) ? (baseURL + '?') : baseURL;
    hashName = (!hashName || typeof hashName !== 'string') ? '' : (hashName.indexOf("#") === -1) ? ('#' + hashName) : hashName;
    var strArray = [];
    for (var prop in this)
    {
      if (typeof this[prop] !== 'undefined' && typeof this[prop] !== 'function' && this[prop] !== null)
      {
        strArray.push([prop, '=', this[prop]].join(''));
      }
    }
    return baseURL + strArray.join('&') + hashName;
  };
  this.buildLink = function (baseURL, linkTxt)
  {
    var url = this.build(baseURL);
    return [ '<a href="', url, '">', ((!linkTxt) ? url : linkTxt), '</a>' ].join('');
  };
}
var $q = new QueryParser(location.search);
</script>


Following is the simple example for above javascript.

EXAMPLE
URL: http://www.mydomain.com/mydir/page.h...&potential=110
$q.name will hold the value 'Super Coder'
$q.rank will hold the value '9999999999'
$q.potential will hold the value '110'
 

Tags Collapse Table Exit Code HTML Javascript Query Parser innerHTML innerHTML.replace Facebook X
Comments 0
Similar posts
  1. Displays the html tags without rendering on a webpage
    352
  2. To hide/show area of web page without re-loading a page
    1,021
  3. To get web browser capability
    527
  4. Photo thumbnail viewer implemented in javascript
    487
  5. To compare two HTML tables on web page in real time
    515
  6. Javascript Confirmation
    594
  7. Javascript Prompts
    453
  8. How to know browser version in javascript ?
    444
  9. How to know the browser name in Javascript ?
    640
  10. Select All, None, Invert checkbox
    507
  11. Photoshop script programming in Javascript
    531
  12. How to return exit code such as exit() function in C/C++ ?
    663
  13. Simple RSS Reader based on javascript
    502
  14. How to find the ip address of a client machine from a web application?
    518
  15. Clipboard copy function working
    3,369
  16. IFRAME Auto Resize in IE, FireFox by Javascript
    456
  17. How to include Javascript from anonther ?
    412
  18. Javascript based Browser Sniffing
    434
  19. Javascript based Regular Expression Tester
    374
  20. Two useful string functions - substr & substring
    409
  21. Is there URLEncode function in Javascript ?
    393
  22. Move window
    548
  23. How to calculate page loading time ?
    360
  24. Number Of Vists
    512
  25. Add Message On Mouse Traling
    2,557
  26. Tooltip
    409
  27. Document Fader
    346
  28. Book Mark This Site
    405
  29. Icon Coupled Url
    427
  30. Button That Will Close The Browser Window
    350
  1. RSS to Array in JavaScript
    2,154
  2. Javascript function to convert sitemap.xml to array
    599
  3. Understanding Elasticsearch from Installation to Trouble Shooting
    913
  4. IndexNow for better SEO
    318
  5. Enhancing SEO for Single Page Applications: Implementing Dynamic URL Updates
    1,041
  6. Method to ignore JavaScript Errors globally instead of try... catch for each of cases
    970
  7. Method to catch the browser console log and errors for remote web debugging
    2,173
  8. Atlassian Jira Ticket API Response Example
    2,769
  9. Three different type of Word-Wrap options in HTML CSS
    1,185
  10. JavaScript code to load XML Document
    602
  11. Creating a Pinterest-Style Card Layout with Bootstrap and Masonry
    596
  12. Equal Height Blocks in Bootstrap with JavaScript
    711
  13. Copy text to clipboard in Javascript
    1,715
  14. To block copy & paste on your blog content
    901
  15. Shake your browser window by Javascript
    937
  16. Put Date Time on HTML
    551
  17. How to elimiate the "Restore Previous Session" dialog when FireFox 2.0 starts
    409
  18. How to block a connection from the certain site ?
    353
  19. Button That Will Show The Page's Source
    388
  20. Add A Scrolling Status Bar Text To Your Site
    409
  21. Add This Page To Favorites
    402
  22. Code To Clear Browser History
    375
  23. Remove the spaces before begin and after end of text
    338
  24. How To Close A Pop-up Window ?
    443
  25. Pop Up Window On Top
    518
  26. How to display message on browser status bar ?
    476
  27. New Window
    606
  28. Firefox Web Tweaking
    376
  29. Color Table
    366
  30. Cursor Changer
    384
Recently updated
  1. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    42
  2. Why SPF and DKIM Are Essential for Linux Email Servers?
    36
  3. How do I create animated background for Google Meet?
    21,044
  4. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    252
  5. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    94
  6. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    157
  7. Array of arguments passed to script in PHP
    90
  8. Invalid Auction Storms in Ad Tech
    98
  9. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    202
  10. The Two Clocks of Ad Server Architecture
    70
  11. Frequency Cap Architecture Overview
    515
  12. Collecting Proof-of-Play from 100,000 Screens
    146
  13. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    288
  14. What is RedisVL?
    109
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    232
  16. TPU & GPU Cluster Management Tools
    245
  17. The Concept of Scaffolding in AI Agents
    234
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    583
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    259
  20. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    339
  21. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    202
  22. Machine Learning: Training vs. Ranking
    222
  23. The Continuous Learning Machine: Designing a Loop That Actually Turns
    222
  24. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    195
  25. Designing a Data Curation System Around a Vector Database
    221
  26. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    256
  27. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    656
  28. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    196
  29. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    219
  30. Understanding Data Lineage
    263