Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add parameter to url

I have a url (e.g. http://www.youtube.com/watch?v=og9B3BEnBHo) and I'd like to add a parameter to it (wmode=opaque) so that its:

http://www.youtube.com/watch?v=og9B3BEnBHo&wmode=opaque

Can anyone tell me which function to use to make this work?

like image 371
manuels Avatar asked Oct 16 '11 16:10

manuels


People also ask

Where is the parameter in URL?

What Are URL Parameters? Also known by the aliases of query strings or URL variables, parameters are the portion of a URL that follows a question mark. They are comprised of a key and a value pair, separated by an equal sign. Multiple parameters can be added to a single page by using an ampersand.

What is a parameter in a link?

URL parameter is a way to pass information about a click through its URL. You can insert URL parameters into your URLs so that your URLs track information about a click. URL parameters are made of a key and a value separated by an equals sign (=) and joined by an ampersand (&).

What is URL parameters HTML?

URL parameters (also called query string parameters or URL variables) are used to send small amounts of data from page to page, or from client to server via a URL. They can contain all kinds of useful information, such as search queries, link referrals, product information, user preferences, and more.

Is it safe to pass parameters in URL?

URLS and query parameters aren't secure. They should never contain sensitive or important information (passwords, static shared secrets, private information, etc).


1 Answers

require 'uri'  uri =  URI.parse("http://www.youtube.com/watch?v=og9B3BEnBHo") uri.query = [uri.query, "wmode=opaque"].compact.join('&')  puts uri.to_s  #edit Since 1.9.2 there are methods added to the URI module  uri =  URI.parse("http://www.youtube.com/watch?v=og9B3BEnBHo") new_query_ar = URI.decode_www_form(String(uri.query)) << ["wmode", "opaque"] uri.query = URI.encode_www_form(new_query_ar) puts uri.to_s 

(The call to String ensures that this also works in the case when the original URI does not have a query string)

like image 183
steenslag Avatar answered Sep 16 '22 20:09

steenslag