0

Hey i am wondering if the following is possible:

There is a single page website for example index.html and a varibale for example MyVariable = 123; within the js script of this index.html. The URL example.com will show the index.html

Now is it possible to share a link that looks like example.com/1234 or something similar which then shows the normal index.html file but it changed the variable MyVariable to "1234" inside the js script?

Thank you! :)

2
  • The only possible way that this could work is if the variable myVariable was not assigned as a local variable to it's function, then you could modify it after page load, but not before, because you would have to modify the file contents. Commented Dec 3, 2013 at 5:16
  • That is fine. And how would i realize this? Can you provide a code? Commented Dec 3, 2013 at 5:17

2 Answers 2

3

First, you'll need to redirect server-side any URL of the form example.com/* to your index.html file. How to do this depends on the server you are using.

For Apache, you'll want to edit your .htaccess file or configuration to add a rewriting rule:

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteRule (.*) index.html [L]
</ifModule>

Source: http://www.josscrowcroft.com/2012/code/htaccess-for-html5-history-pushstate-url-routing/

Then, in you JavaScript code, you can access the current location (URL) via the window.location object. You'll be interested in the pathname property, that will contain in your example: /1234.

To extract the value you'll have to do something like this:

var MyVariable = window.location.pathname.substring(1);
Sign up to request clarification or add additional context in comments.

8 Comments

Actually, you would want to omit the var declaration.
That sounds really reasonable. Can you give me further information how i could redirect the urls?
@Ohgodwhy I think one can understand whether to put var or not. For the sake of the example, not putting var would be a very bad practice.
@MarcSter Which server are you using?
Do you mean it is only possible without the var declaration, but it is unsafe to run a website like that? I am using an Apache
|
1

i would recommend using php get , if you are using apache , where you can transfer your variable through a link

call you page like index.php?myvariable=1234

<html>
<body>

your variable is <?php echo $_GET["myvariable"]; ?>


</body>
</html>

http://www.w3schools.com/php/php_forms.asp

3 Comments

Sounds good, how would i get the value of that PHP variable into my js variable?
simply using var my_updated_variable = "<?php echo $_GET["myvariable"];?>";
Thank you very much! The best solution for my purpose! But i will accept the answer of ngrymen because the question was about doing it with javascript.. but thank you anyway!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.