2

How to redirect User in No.1 html page to another No. 2 html page using javascript when a user visit first time in No.1 page. If possible pls help me.

Abu Raihan
  • 21
  • 3
  • 1
    Depends if first time means first time ever or first time this session. If it's the later you should use a session, otherwise a cookie should do the job. You have shown nearly no effort in asking this question. What have you tried? Code please. – StackSlave Nov 17 '15 at 09:30

4 Answers4

1

Just use cookie to mark the user first time and redirect it with document.location:

if ( getCookie( 'first-time' ) === undefined ) {
    setCookie( 'first-time', 'yes' );
    document.location = '/second-page-url';
}

To use cookie read this Set cookie and get cookie with JavaScript and this Get cookie by name

Community
  • 1
  • 1
Legotin
  • 2,378
  • 1
  • 18
  • 28
0

You need to set a cookie on the first visit, then you can check the user was there before on your site.

online Thomas
  • 8,864
  • 6
  • 44
  • 85
0

To check 1st visit or not one can use cookie or localStorage.
And to redirect there are several methods/mechanisms: this article explains it well

Nachiketha
  • 21,705
  • 3
  • 24
  • 32
0

Agree with legotin, but you will need to read/write cookie somehow:

Read cookie:

function getCookie(name) {
      var matches = document.cookie.match(new RegExp(
        "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"));
      return matches ? decodeURIComponent(matches[1]) : undefined;
    }

Set cookie:

function setCookie(name, value, options) {
  options = options || {};

  var expires = options.expires;

  if (typeof expires == "number" && expires) {
    var d = new Date();
    d.setTime(d.getTime() + expires * 1000);
    expires = options.expires = d;
  }
  if (expires && expires.toUTCString) {
    options.expires = expires.toUTCString();
  }

  value = encodeURIComponent(value);

  var updatedCookie = name + "=" + value;

  for (var propName in options) {
    updatedCookie += "; " + propName;
    var propValue = options[propName];
    if (propValue !== true) {
      updatedCookie += "=" + propValue;
    }
  }

  document.cookie = updatedCookie;
}
Vnuuk
  • 6,177
  • 12
  • 40
  • 53