HTML passing string from one page to another - javascript

i am developing a mobile application and i need to send a certain string where i can be able to use it in an HTML page and i wanna send it from a previous HTML page ,, what's the simplest way to do so ?. I have two pages index.html
and home.html and i want to send a variable such as number or string from the index page to the home, as i will change the design of the home page upon this variable. is there any idea how can i do it ? Thanks in Advance.

If you a using a server side language, A common approach is to use a query string
For example your URL would look like this:
Home.html?myvariable=myvalue
In PHP the way to access the value of myvariable is to use $_GET['myvariable']

Related

How do I transform a regular url into a variable?

If i have somesite.com/thisiswhatiwant, how can I transform that into a variable and process it, without using get vars? What should I google in the first place?
The idea is to create a dynamic page structure where that part of the url will populate variables in the page and be used to return dynamic page specific queries.
Is there a framework I can use that has a way to handle this easily?
If I use javascript for this, how should I handle it to not return any 404 errors but rather just pull a templating page and then use that part of the url for developing of the page?
Thank you!
Here is how you parse the path of a url in PHP
$url = "http://somesite.com/thisiswhatiwant";
var_dump(parse_url($url, PHP_URL_PATH));
If i have somesite.com/thisiswhatiwant, how can I transform that into a variable and process it, without using get vars? What should I google in the first place?
That's simply getting the current URL and parsing it. (Which are pretty well covered in the linked questions).
You do need to get the server to execute the PHP first. This question about the front controller pattern explains that.
If I use javascript for this, how should I handle it to not return any 404 errors but rather just pull a templating page and then use that part of the url for developing of the page?
Assuming you mean client-side JavaScript: You can't.
JavaScript runs in the context of a webpage.
Get page from server
Parse HTML document
Run JavaScript that page says to run
If you 404 at step 1 then everything stops and no JS runs.
The correct terminology is vanity URLs. They are static urls that behave like dynamic urls. Dynamic urls are urls with queries which is not what we want here.
This tutorial will help.
The solution is trough the .htaccess rules as i expected.
The rest is basic php/db queries.
I still do not know of a web app framework that makes this trivial to implement, but there must be.
Here is the tutorial
http://culttt.com/2011/11/16/how-to-make-vanity-urls-using-php-htaccess-and-mysql/

What's the best way to amend a url via javascript?

My wordpress site has a search form that is crap.
It produces this search string: www.mysite.com/properties/?wpp_search[sort_order]=DESC&wpp_search[sort_by]=price&wpp_search[pagination]=on&wpp_search[per_page]=10&wpp_search[strict_search]=false&wpp_search[property_type]=residential_sales%2Cresidential_lettings%2Ccommercial%2Cfine_living&wpp_search[area]=&wpp_search[price][min]=&wpp_search[price][max]=&wpp_search[branch]=-1&wpp_search[bedrooms]=-1&wpp_search[department]=-1#propertycontent
What's the best way to grab this string, and change certain aspects eg:
[sort_order]=ASC
[sort_by]=price
Then reload the page displaying the new string and thus changing the form output?
Try this.
url.replace('[sort_order]=DESC','[sort_order]=ASC').replace('[sort_by]=price','[sort_by]=newPrice');
You can get the url's query portion using location.search. Look for the parameter, modify, reconstruct your url and assign that value to location to load the page with that url.

How do I Transfer values from one Javascript file to another JS file without the need of server interaction nor cookies?

I want to transfer a variable value without the need of cookies nor server interaction in JS, is ther any way possible ? for instance if i have foo.js and a variable fooVar = 10, and bar.js and barVar is there any way possible for barVar = fooVar without cookies?
Based on your comments, you could store the variable you want to access in the second page in the query string of the second page, or in the hash of the query string of the second page. Use that query string to load the second page.
e.g. in the query string:
http://example.com/bar.html?myvar=5
or e.g. in the hash of the query string:
http://example.com/bar.html#?myvar=5
Using the hash is likely to be the best option as your server will most likely ignore whatever you put in the hash (unless the server is programmed to do otherwise).
You can then use window.location.hash in the javascript loaded by bar.html to get the hash string and decode it to get your stored variable...
You can use HTML5 localStorage. The localStorage saves your data into the web browser. The difference with $_COOKIE is that data is accessible only via web browser with javascript and they are not sent in every HTTP request. Here is a link with examples.
You say that you want to carry the value of the variable from one page to the next page.
Why do you not use the url?
Add something like ?fooVar=10 to the url of the second page and then parse the url with the second script on the second page.
Here is a example how you can parse the url in the second script
how can i get query string values

Special URL for language selection?

Just a simple question, I was wondering why some websites have something like "?lang=EN" in their URL after selecting a language? Is it because their html file or folder containing it is named "?lang=EN", or some other code that does this? I'd like to set the URL like that for my website (has 2 languages). Currently I have folder structure like this:
Language selection: D:/media/index.html
EN site: D:/media/en/index.html
CN site: D:/media/cn/index.html
Files for the website: D:/media/site
Thanks.
First of all, anything after the file extension ( .html ) is a server side function.
The ? is a function for PHP and adds variables to the super global GET array ( in the form: ?variable=value&variable2=value2 ) that is directed to from another page and from that point many things can be done with the data.
Sites that use the ?lang=EN are probably programmed to print out the chunks of text needed on the single page in the places and languages required. Though it is possible using this method to redirect to a language specific directory.
Hope this helps :)
That's because they often have a content management system where the content isn't stored in files necessarily, but in a database. The lang=en is a GET variable from the URL that they retrieve in, for example, PHP, to display the correct content. In your case, however, you can just redirect the user if they click EN or CN to the appropriate locations, in your case, /en/index.html and /cn/index.html.
The url you see at the address bar, whatever comes after "?" is called "QueryString" and with libraries on the server side (based on the developing platform that website is made on) you can access the values. For instance the value of "lang" can be equal to "EN" or "CN" etc.
By the way you can have some http handlers to rewrite the requested url and get your parameters through the url that physically doesn't exists. Like the one you mentioned, "http://yoursite.com/en/default.whatever". I myself prefer this way but as you requested you should use some server side libraries to access the query string values and choose the language of the content you wanna send to client.
Also as one solution that once I used, you can also use some translation service (like translate.google.com) client libraries and call it at client side with jquery or even javascript and translate all the texts on page load. Although it's damn fast in action, it has some issues you will see.
Hope it helps.
PHP uses $_GET to get value from variables from the URL.It gets the value from that LANG variable and then it selects all from a file where are stored all the words in different languages or from the database
You don't need to copy every file and then translate it.
Search for php dynamic pages tutorial in your case. I found THIS.
P.S. PHP is one from many ways to do this.

HTTP cookie between two HTML pages

I have two HTML pages. After entering few inputs users will be redirected from first page to second page. Before redirecting the user to second HTML page(using window.location="new HTML URL"), I persist few of the user inputs in cookie using document.cookie DOM API.
When I am in the second HTML page, I could not retrieve the value from this cookie. I think since document object would have changed in the new HTML page, my cookie values become inaccessible.
Can someone tell me: how do I retrieve the value from a cookie persisted by one javascript in one HTML page in other HTML page i.e cookie written by HTML A's javascript in HTML B's javascript?
I don't have any server-side code, so I could not take advantage of server-side logic. Also I am not supposed to pass the values in URL. So I need a solution on plain javascript and HTML.
If some one has a better solution please let me know. Thanks
try to use localStorage instead of cookies,
// set your values in the first page
localStorage.setItem('itemKey', 'values');
// on the second page, retrieve them
var values = localStorage.getItem('itemKey');
you can use a jStorage plugin for cross browser behaviour.
also refer to this question for storing objects instead of strings
JAAulde is on point with his answer.
For what the OP is trying to do something like PHP would be great, in that case I wouldn't bother with cookies in order to just pass data between two pages, that's just silly. However, if true persistence was needed and the data requirements were simple cookies would be the way to go even while using a language such as PHP.
Those are rather draconian constraints, is this a class project? That said there aren't any other ways to do what you're attempting, save for an ugly and highly insecure hack of the DOM.

Categories

Resources