Make class combinator open page in separate tab

Asked

Viewed 84 times

2

I have a code in which he creates me a page as if it were a theme, picking randomly the css classes, but what he does is create the "theme" within the page that this button, and would like it opened up a new page with the selected theme.

Follow the code js

Code that generates the classes

var classesRM = ['fundoRomantico', 'fundoRomantico1', 'fundoRomantico2'];
var classestopoRmt = ['topo-romantico', 'topo-romantico1', 'topo-romantico2'];
var classesmenuRmt = ['romantico-menu-01', 'romantico-menu-02', 'romantico-menu-03'];
var classesfootRmt = ['footer-romantico', 'footer-romantico1', 'footer-romantico2'];

var classesFontTexto = ['font-romantico', 'font-romantico1', 'font-classica', 'font-classica1', 'font-moderno', 'font-moderno1'];
var classesFontTitulo = ['font-titulo-romantico', 'font-titulo-romantico1', 'font-titulo-classica', 'font-titulo-classica1', 'font-titulo-moderno', 'font-titulo-moderno1'];
var classesMenu = ['menu-01', 'menu-02', 'menu-03'];
var classesTexto = ['textoBranco', 'textoPreto', 'textoCinza'];
function sorteia(arr) {
    var rand = Math.floor(Math.random() * arr.length);
    return arr[rand];
}

Button code

document.getElementById('troca').addEventListener('click', function() {
    var classeRM = sorteia(classesRM);
    var classetopoRmt= sorteia(classestopoRmt);
    var classemenuRmt= sorteia(classesmenuRmt);
    var classeFontTexto = sorteia(classesFontTexto);
    var classeFontTitulo = sorteia(classesFontTitulo);
    var classeTexto = sorteia(classesTexto);
    var classeMenu = sorteia(classesMenu);
    var elemento = document.getElementById('navbar');
    elemento.className = classemenuRmt;
    var elemento = document.getElementById('nav-bar');
    elemento.className = classeMenu;
    var elemento = document.getElementById('titulo');
    elemento.className = classeFontTitulo;
    var elemento = document.getElementById('topo');
    elemento.className = classetopoRmt;
    var elemento = document.getElementById('body');
    elemento.className = classeRM + ' ' + classeTexto + ' ' + classeFontTexto;
}); 
  • You want me to open a new page or on the same page?

  • I want you to open in a new page, like the buttons stay on a screen and when you click the generated theme open in a different page without the buttons

  • Okay, and the content of that page is the same as the first?

  • Yes yes, because this is to be the model, then I manage to get it to open in new page, I already start to see the content

1 answer

1

If you change your logic a little you can do as you please.

Use Event Handler only to open a new page:

document.getElementById('troca').addEventListener('click', function() {
  var win = window.open(location.origin, '_blank');
  win.focus();
}); 

and then within each page run the rest of the code in the page load:

window.onload = function(){
    var classeRM = sorteia(classesRM);
    var classetopoRmt= sorteia(classestopoRmt);
    // etc...

The complete code could be:

(function () {
    var classesRM = ['fundoRomantico', 'fundoRomantico1', 'fundoRomantico2'];
    var classestopoRmt = ['topo-romantico', 'topo-romantico1', 'topo-romantico2'];
    var classesmenuRmt = ['romantico-menu-01', 'romantico-menu-02', 'romantico-menu-03'];
    var classesfootRmt = ['footer-romantico', 'footer-romantico1', 'footer-romantico2'];

    var classesFontTexto = ['font-romantico', 'font-romantico1', 'font-classica', 'font-classica1', 'font-moderno', 'font-moderno1'];
    var classesFontTitulo = ['font-titulo-romantico', 'font-titulo-romantico1', 'font-titulo-classica', 'font-titulo-classica1', 'font-titulo-moderno', 'font-titulo-moderno1'];
    var classesMenu = ['menu-01', 'menu-02', 'menu-03'];
    var classesTexto = ['textoBranco', 'textoPreto', 'textoCinza'];

    function sorteia(arr) {
        var rand = Math.floor(Math.random() * arr.length);
        return arr[rand];
    }
    window.onload = function () {
        document.getElementById('troca').addEventListener('click', function () {
            var win = window.open(location.origin, '_blank');
            win.focus();
        });

        var classeRM = sorteia(classesRM);
        var classetopoRmt = sorteia(classestopoRmt);
        var classemenuRmt = sorteia(classesmenuRmt);
        var classeFontTexto = sorteia(classesFontTexto);
        var classeFontTitulo = sorteia(classesFontTitulo);
        var classeTexto = sorteia(classesTexto);
        var classeMenu = sorteia(classesMenu);
        var elemento = document.getElementById('navbar');
        elemento.className = classemenuRmt;
        var elemento = document.getElementById('nav-bar');
        elemento.className = classeMenu;
        var elemento = document.getElementById('titulo');
        elemento.className = classeFontTitulo;
        var elemento = document.getElementById('topo');
        elemento.className = classetopoRmt;
        var elemento = document.getElementById('body');
        elemento.className = classeRM + ' ' + classeTexto + ' ' + classeFontTexto;
    }
})();

It would be interesting to optimize this code to not be so extensive. That is to do the same but with fewer lines of code. I didn’t touch it, just another question.

  • The window.onload code I place it where?

  • @Victorsaul edited the answer. So you can put anywhere inside the HTML.

  • Sorry but this code did not work, it opens directly the screen with the theme and when you click the button to do the shuffle it opens the screen but without content

  • @Victorsaul do you have it online? or can you recreate the problem in a jsFiddle with your code so I can help more?

  • I have in jsFiddle an example of my code, of course in my place it is more elaborate with images in bg and such but its structure is this https://jsfiddle.net/jpcaja7t/18/

  • @Victorsaul I think I should have used .pathname. Is that how you want it? -> http://jsfiddle.net/p2rmre5z/1/show/

  • This, however I do not want the buttons to appear on the screen that was generated, I want the buttons to be on a screen and when clicking it generates the theme without the buttons

  • @Victorsaul something else... this code is giving me pain to see... can I change the HTML? Keeping it will be impossible...

  • @Victorsaul spent some time doing this -> http://jsfiddle.net/jafxrpkv/ I think the code should look like this. I don’t have time to look at the rest anymore. But at the bottom you can pass the style url in a query string and JS will see if there is something to read and hide or not the buttons. Tomorrow I’ll take a look here again.

  • Okay, I just really need you to open the theme you give me in a new tab without showing the buttons

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.