TinyPortal

Development => Block Codes => Topic started by: diegolyanky on October 21, 2013, 06:08:35 PM

Title: Open url in new tab HELP NEEDED
Post by: diegolyanky on October 21, 2013, 06:08:35 PM
Hi, i'm trying to make a block with this code:

<input name="msg" type="text" /><button>DALE AL CLICK</button>

<script>
    document.querySelector('button').addEventListener('click', function() {
        var msg = document.querySelector('input[name=msg]').value.replace(/ /g, '+');
        location.href = 'http://www.misitio.com/index.php?action=profile;u=' + msg;
    });
</script>




The only I need, is OPEN the url in a new tab, is this possible ?
Title: Re: Open url in new tab HELP NEEDED
Post by: IchBin on October 21, 2013, 09:00:54 PM
Browsers treat new windows differently. Some create a new window. Some create a new tab. I don't know of any way that works for all browsers. Besides, you are likely going to piss someone off that like the default behavior of their browser when opening a new link. Your better off just creating a new window and leaving it up to the browser default setting of how it treats new windows vs tabs.

window.open(url,'_blank');

Title: Re: Open url in new tab HELP NEEDED
Post by: diegolyanky on October 21, 2013, 09:11:16 PM
Ok...

so... Where can I put the "_blank" ?

That's all I need. Otherwise, if can open in a new window is better for me. ;)

Thanks a lot for helping me :)
Title: Re: Open url in new tab HELP NEEDED
Post by: IchBin on October 21, 2013, 09:20:46 PM
I already showed you in my last post. :)
Title: Re: Open url in new tab HELP NEEDED
Post by: Maxx1 on October 21, 2013, 09:29:14 PM
I'll spell it out, but some scripts may not allow this:

<a target="_blank" href="http://www.mysite.com">link</a>


regards,
Maxx
Title: Re: Open url in new tab HELP NEEDED
Post by: diegolyanky on October 21, 2013, 09:32:01 PM
IchBin, Sorry ... I didn't see it.

But, using my code...

Is this ok ? ( Because, don't work... steel opening in same window/tab )


<input name="msg" type="text" /><button>BUSCAR</button>

<script>
    document.querySelector('button').addEventListener('click', function() {
        var msg = document.querySelector('input[name=msg]').value.replace(/ /g, '+');
window.open(http://www.MYSITE.com/index.php?action=profile;u= + msg,'_blank');
    });

</script>


Maxx1: Thanks, but I need to know where to put the _blank... I don't know where... :(
Title: Re: Open url in new tab HELP NEEDED
Post by: IchBin on October 21, 2013, 09:44:12 PM
You forgot a couple of single quotes in your window.open.
window.open('http://www.mysite.com/index.php?action=profile;u=' + msg, '_blank');

That code opens a new tab for me in Chrome and Firefox.
Title: Re: Open url in new tab HELP NEEDED
Post by: diegolyanky on October 21, 2013, 09:49:52 PM
IchBin:

Sorry, but don't work...

Is this code ok ?



<input name="msg" type="text" /><button>BUSCAR</button>

<script>
    document.querySelector('button').addEventListener('click', function() {
        var msg = document.querySelector('input[name=msg]').value.replace(/ /g, '+');
window.open("http://www.mysite.com/index.php?action=profile;u=' + msg, '_blank');
</script>

Title: Re: Open url in new tab HELP NEEDED
Post by: diegolyanky on October 21, 2013, 10:13:51 PM
Hello ? :(
Title: Re: Open url in new tab HELP NEEDED
Post by: IchBin on October 21, 2013, 11:40:15 PM
Sorry there was a double quote where there shouldn't be. Try mine again in my last post.
Title: Re: Open url in new tab HELP NEEDED
Post by: diegolyanky on October 22, 2013, 05:00:04 PM
Yes, sorry for double post...

I used your code, but i'm not sure if is ok or not (maybe bad syntax)


<input name="msg" type="text" /><button>BUSCAR</button>

<script>
    document.querySelector('button').addEventListener('click', function() {
        var msg = document.querySelector('input[name=msg]').value.replace(/ /g, '+');
window.open("http://www.mysite.com/index.php?action=profile;u=' + msg, '_blank');
</script>



Is ok ? ... Don't works for me ... :(
Title: Re: Open url in new tab HELP NEEDED
Post by: IchBin on October 22, 2013, 08:17:41 PM
You cannot have a mixture of double and single quotes like that. If you take the line I posted above here:
http://www.tinyportal.net/index.php?topic=35241.msg281392#msg281392

Replace the line in your code with that line and it should work.
Title: Re: Open url in new tab HELP NEEDED
Post by: diegolyanky on October 23, 2013, 05:24:23 PM
Quote from: IchBin™ on October 21, 2013, 09:44:12 PM
You forgot a couple of single quotes in your window.open.
window.open('http://www.mysite.com/index.php?action=profile;u=' + msg, '_blank');

That code opens a new tab for me in Chrome and Firefox.

Is not working for me...


so was the code, with the recommendation that you have given me.

Is my code as well?

<input name="msg" type="text" /><button>BUSCAR</button>

<script>
    document.querySelector('button').addEventListener('click', function() {
        var msg = document.querySelector('input[name=msg]').value.replace(/ /g, '+');
window.open('http://www.ecuforo.com/index.php?action=profile;u=' + msg, '_blank');
</script>


What we do not know is if I put the code you gave me in the right line.
Title: Re: Open url in new tab HELP NEEDED
Post by: IchBin on October 23, 2013, 07:38:37 PM
You put the line in the right location. Only it seems you have removed some code. You are missing the closing }); for the addEventListener function. Try this code.

<input name="msg" type="text" /><button>BUSCAR</button>

<script>
    document.querySelector('button').addEventListener('click', function() {
        var msg = document.querySelector('input[name=msg]').value.replace(/ /g, '+');
window.open('http://www.ecuforo.com/index.php?action=profile;u=' + msg, '_blank');
    });
</script>
Title: Re: Open url in new tab HELP NEEDED
Post by: diegolyanky on October 23, 2013, 07:53:07 PM
Yeah !!...
It works !!

Thanks IchBin... ( gift for you, check your email ;) )