0

I've been searching online for an answer to this but I can't seem to find anything which can help.

I was wondering if its possible to open a index.html file with an extension after the .html

(for example it would open a file like this - index.html?lc=uk) automatically when you would double click the file or when you click on a link which connects to that file.

Hope that makes sense.

If anyone could help would be much appreciated.

Regards, Seb

user2072826

    function setGetParameter(paramName, paramValue)
{
    var url = window.location.href;
    if (url.indexOf(paramName + "=") >= 0)
    {
        var prefix = url.substring(0, url.indexOf(paramName));
        var suffix = url.substring(url.indexOf(paramName));
        suffix = suffix.substring(suffix.indexOf("=") + 1);
        suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
        url = prefix + paramName + "=" + paramValue + suffix;
    }
    else
    {
    if (url.indexOf("?") < 0)
        url += "?" + paramName + "=" + paramValue;
    else
        url += "&" + paramName + "=" + paramValue;
    }
    window.location.href = url;
}

and then in the body tag:

<body onload="setGetParameter('lc', 'uk');">

This has worked but the problem is that it keeps refreshing the page constantly. Is there a way to stop the refreshing?

Roberto Maldonado
  • 1,585
  • 14
  • 27
Sebbie
  • 81
  • 1
  • 11

2 Answers2

1

Unfortunately this does not work. You can not pass URL Parameters into the file-name.

If you want to add it as the page loads, you could add this to the JavaScript of the page:

function setGetParameter(paramName, paramValue)
{
    var url = window.location.href;
    if(!(url.indexOf(paramName) >= 0))
    {
      if (url.indexOf(paramName + "=") >= 0)
      {
          var prefix = url.substring(0, url.indexOf(paramName));
          var suffix = url.substring(url.indexOf(paramName));
          suffix = suffix.substring(suffix.indexOf("=") + 1);
          suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
          url = prefix + paramName + "=" + paramValue + suffix;
      }
      else
      {
      if (url.indexOf("?") < 0)
          url += "?" + paramName + "=" + paramValue;
      else
          url += "&" + paramName + "=" + paramValue;
      }
      window.location.href = url;
    }
}

and then in the body tag:

<body onload="setGetParameter('lc', 'uk');">

Original Source(Yes, there is a difference between the code)

Community
  • 1
  • 1
user2072826
  • 528
  • 1
  • 5
  • 12
  • Hi user2072726, it doesn't seem to have work, I've added the code to the html file but it doesnt seem to change anything when I've opened the file. – Sebbie Jun 22 '15 at 15:18
  • Sorry ignore previous comment, I have solved why it wasn't work and its working :) - but the only problem is that it keeps refreshing the page.. Anyway to stop that? – Sebbie Jun 22 '15 at 15:20
  • Okay, I added a second piece of code that should prevent the page from reloading. Just replace what I have given you already with the edit. – user2072826 Jun 22 '15 at 15:38
  • I've changed it to the new edit but now it doesnt add the extension like the previous code. :( – Sebbie Jun 22 '15 at 15:40
  • I've doubled checked everything but it doesnt seem to add the extension to the url :'( – Sebbie Jun 22 '15 at 15:51
  • you have the script located after the body tag? – user2072826 Jun 22 '15 at 15:56
  • I've solved the problem :) - it was another script file interfering with it.. Thank you very much for helping XD (Virtual Hug) – Sebbie Jun 22 '15 at 16:03
0

If you are using Apache, you may use mod_rewrite to perform almost any kind of "magic" with request file names. As long as you know home to use regular ecpressions, of course. I suggest you to do some research on mod_rewrite.

Ed de Almeida
  • 3,675
  • 4
  • 25
  • 57