Jump to content
Sign in to follow this  
igstudders

ReactJS redirect

Recommended Posts

I am attempting to use a meta tag to redirect someone if they attempt to go to a specified number of pages. Problem is these pages are pdf files so I cant really add some code to that. Anyway I decided to use a javascript file and import React and ReactDOM from the JS Library. I simply don't know what I am doing wrong with this code though. If anyone could help that would be amazing!

import React from 'react';
import ReactDOM from 'react-dom';

String[] Directory = {"A", "Bunch", "Of", "Website", "Directories", "Here"};
if (window.location.href = "Website name here" + Directory)) {
    ReactDOM.render(
      <META http-equiv="refresh" content="0;URL=Index.html">,
      document.getElementById('app')
    );
};

Much Appreciated to whoever can help!

Share this post


Link to post
Share on other sites

This does not appear to be browser Javascript, which does not support import statements or typed arrays.

It seems like there would be an issue in your render() call because your string is not quoted.

ReactDOM.render(
  '<META http-equiv="refresh" content="0;URL=Index.html">', // Added quotes to the string
  document.getElementById('app')
);

 

Share this post


Link to post
Share on other sites
Quote

It seems like there would be an issue in your render() call because your string is not quoted.

Welcome to ReactJS, a Javascript library designed to look completely wrong to existing Javascript programmers.

Because of that, and since I'm not familiar enough with ReactJS, I don't know if these lines are correct:

 

Quote

String[] Directory = {"A", "Bunch", "Of", "Website", "Directories", "Here"};
if (window.location.href = "Website name here" + Director

Should that be "==" instead of "="?  If an array is concatenated on the end of a string, is that trying to test each array element or is it going to join them?  Is the default to join them with slashes to produce a directory structure?  I don't know how the developers of Reach chose to answer those.

Share this post


Link to post
Share on other sites

This is unfamiliar to me, since other Javascript frameworks and libraries I've worked with did not actually alter Javascript syntax.

If this really is a browser-side library, it seems pretty inefficient to parse all that code just to have a prettier syntax. It would have to be compiled into real Javascript, either using a server-side compiler or during runtime with a Javascript program.

Share this post


Link to post
Share on other sites

I would agree with a couple things mentioned

  1. The syntax is not correct (regardless of React usage)
  2. The need for React is not apparent

 

That said, it certainly could be done with React, or "vanilla" JS would certainly be more practical as this "simple" case seems warrants.  

 

As far as implementation details go, would a better option be to increment a counter each time a link / page is visited in a cookie or with one of the web storage APIs?  Either way a user can work around it, but at least one is a more "stateful" solution.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×