Javascript url management - javascript

I need help because I am trying to create a lib with javascript in order to create, modify, delete params in my url, I explain :
www.mydomain.com/thing/?id=1&et=67&type=chercher
How u can see, my params are random, it's dynamic, It's not every time the same url, I had this url, one time I can have ?id=1&et=67&type=chercher and other time I can have ?id=1&type=chercher or for example just ?id=1 or others params.
So it's not easy because of user action, the url with params will change, so is there an lib or an application written with javascript which can do this easily ?
Thx everyOne for your futures responses !!!!

You are creating a library to manipulate URLs? Have you had a look at the existing solutions?
URI.js is something I can recommend. In its Readme URI.js links to a bunch of alternative solutions you could look into as well.

Yes, absolutely. You can use the jQuery URL Parser plugin. Just make sure you include also jQuery in your page and not only the plugin.

Do u need library which manipulate URL string?
pass associative array(HASH) to function {a:2, b:3, c:4} and then go through this hash and form string.
Is this what u need?
To update - split string by & and downparse into HASH, and replace value by H['a'] = 4;
To delete - delete H["a"];
But better do not reinvent bicycle use tested solutions: http://code.google.com/p/jsuri/

Related

How can I parse a JSON file with comments in client-side Javascript? (Don't need to preserve comments.)

First, I know that JSON doesn't support comments. I'm using them anyway because I need to write little notes to make it easy for people who aren't tech-savvy to edit this file.
I'm using double-slash comments, but I can switch to slash-star if needed.
"campaignID": "230109-stock-shop", // this is the method I want
I know the technique of creating extra comment fields like this, but it doesn't fit my needs.
"__comment": "this ISN'T the method I want"
I'm working entirely in client-side Javascript/jQuery. When I import a file with comments, it - of course - doesn't work.
$.getJSON('file.json', function (json) {//do things})
Is it possible to strip out the comments when importing the JSON file somehow? I tried to find a JS library or something that would do that, but everything I found is server-side, and I need everything to run client-side.
I'm also not super great with Javascript, so it's possible I'm missing something obvious.
I really appreciate any help you can give!
You can remove all the comments before trying to parse it.
You won't be able to use $.getJSON(), since that tries to parse the JSON itself. Use $.get(), remove the comment and parse it.
$.get('file.json', function(commented_json) {
json = JSON.parse(commented_json.replace(/\/\/.*/g, ''));
// do things
}, "text");
Note that this won't work properly if any of the strings in the JSON contain //. That would require using a real JavaScript parser so it can distinguish the context.

Have multiple URIs with parameters in routes.js using MEAN

I want to be able to use get methods in my MEAN API, i have the following code in my index.js:
router.route('/platillos/:id')
.get(PlatilloCtrl.getPlatillosById)
.post(upload.array(),PlatilloCtrl.addComentario)
.put(upload.array(),PlatilloCtrl.updatePlatillo)
.delete(PlatilloCtrl.deletePlatillo);
and I made this one too:
router.route('/platillosC/:categoria')
.get(PlatilloCtrl.getPlatillosByCategoria);
I would like to have the category and the id in the same URI withouth having an extra one.
Is there a way to do this?
Without knowing exactly what you're using in terms of a routing solution, most libraries will accept and parse multiple parameters, ie:
router.route('/platillosC/:id/:categoria')

What does 'x' mean as the last letter in `src` attribute

In some projects I noticed that javascripts included into HTML like this:
<script type="text/javascript" src="./js/score.js?x"></script>
What's mean last 'x' symbol?
It is a query string which may be used to pass variables to the script.
It will help to overcome cache problems as well.
If I had to guess, I would say the X is being used as a querystring fragment. Unless the server is depending on the fragment being there, it could possibly be used as a cache buster.
Essentially, by changing that X to a Y we could make the browser fetch a fresh copy. This is useful if you need to make sure users get a new copy of a file.
Of course, without talking to the author we are just guessing. Perhaps the server needs it there to properly build the file in the first place. Or maybe the javascript itself is using it.
It is not a symbol. It is a piece of query string like on web scripts it would be something like test.php?reload=true
Such techniques might be helpful to overcome the caching problem as mentioned by SLaks on the comments.
You can pass parameters to your javascript.
It can be used to initiliaze variable or be used somewhere.
Take a look at this link http://feather.elektrum.org/book/src.html

Using a full URL with Restangular

I like all of the functions of Restangular for AngularJS, except that I can't find (and it may not support) a way of just passing a full URL to it. I realize the benefit of the .one('something','someparam') but my issue is that I'm passed various URL strings, and I really don't want to have to split() them just to use Restangular.
Also, I know about the baseURL function; but these URLs I'm being passed don't necessarily derive from the same base path.
For example, I might have:
/us/en/product/17726
/us/es/products
/us/es/product/991A0
/ca/en/accounts
All I'm given are the Strings...
I'm the creator of Restangular.
You have 2 options to do that.
1) Creating scoped Restangulars with different BaseURLs in each case: https://github.com/mgonto/restangular#how-to-create-a-restangular-service-with-a-different-configuration-from-the-global-one
2) You can use it like Restangular.all('us/en/').one('products', 1726) or Restangular.one('us/en/product', 1234)
Hope it works for you :)
I needed to simply GET an absolute url and came across this question.
Here's how:
Restangular.oneUrl('routeName', 'http://absolute.url').get();
restangular#restangular-methods

JavaScript: get and set variables in window.location.hash?

Does anyone have a good solution for getting and setting variables in window.location.hash?
Take a URL that looks like this:
domain.com/#q=1&s=2
What I'd like is an unstressful way - JavaScript or jQuery - to check the values of q and s when the page loads, and change them following events on the page.
I have found some code for getting hash variables, but nothing sensible for setting them.
Am I missing something really obvious, or do I need to roll my own solution (and release it!)?
Thanks.
Haven't used it but there is jHash
jHash allows you to work with the
'location.hash' value in a similar
fashion to a server-side query string.
This library utilizes the HTML5
"onhashchange" event, but also
includes a fall back to still allow
the change notifications to work
properly in older web browsers.
jQuery BBQ can do this.
See also:
Get URL parameter with jQuery
Get QueryString values with jQuery
Edit as #gonchuki points out, jQuery.query can also do this.
JHash didn't work for me in that I wanted it to trigger the routes right away. I personally used routie instead.
It lets you do advanced routing just like jHash but will trigger on page load correctly.
Below will match example.com/#users/john
routie('users/:name', function(name) {
//name == 'bob';
});

Categories

Resources