Author Topic: Really simple static html page  (Read 770 times)

0 Members and 1 Guest are viewing this topic.

Offline firewalkerTopic starter

  • Super Contributor
  • ***
  • Posts: 2450
  • Country: gr
Really simple static html page
« on: October 18, 2022, 02:15:48 pm »
In an html list as the example bellow. Can I add a button to open the selected page? I know about the onchange event. I want to do it after pressing a button.

Code: [Select]
<!DOCTYPE html>
<html>
<body>

<h1>Select</h1>
<br>
<form>
<select name="Choose">
    <option value="https://.....">One</option>
    <option value="https://.....">Two</option>
  </select>
</form>
</body>
</html>
« Last Edit: October 18, 2022, 02:17:39 pm by firewalker »
Become a realist, stay a dreamer.

 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5895
  • Country: es
Re: Really simple static html page
« Reply #1 on: October 18, 2022, 02:34:11 pm »
I barely know html yet it took me two searches in Google:
https://bfy.tw/Tf2v

Code: [Select]
<html>
<body>
<h1>Select</h1>
    <p>Open in the same window</p>
    <a href="http://www.google.com" target="_parent"><button>Google</button></a>
    <a href="http://www.yahoo.com" target="_parent"><button>Yahoo!</button></a>
    <br><br>
    <p>Open in a new window</p>
    <a href="http://www.google.com" target="_blank"><button>Google</button></a>
    <a href="http://www.yahoo.com" target="_blank"><button>Yahoo!</button></a>
</body>
</html>
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline firewalkerTopic starter

  • Super Contributor
  • ***
  • Posts: 2450
  • Country: gr
Re: Really simple static html page
« Reply #2 on: October 18, 2022, 02:47:01 pm »
I didn;t palce the question correctly. I have a drop down list that you can select an item. The value of the item is a url link. I want to also place a button that will open the selected url.
« Last Edit: October 18, 2022, 02:49:59 pm by firewalker »
Become a realist, stay a dreamer.

 

Online mariush

  • Super Contributor
  • ***
  • Posts: 5016
  • Country: ro
  • .
Re: Really simple static html page
« Reply #3 on: October 18, 2022, 02:57:55 pm »
You would do it with Javascript 

Place an onclick event on the button, which calls a Javascript function which reads the value configured in the select, and based on that select value it opens the desired URL.

// event.preventDefault() kills the submit form event triggered by pressing the only button in the form (default in some if not all browsers)
// jump is the function

I guess you could use onchange to auto jump without having to press button.

Code: [Select]
<!DOCTYPE html>
<html>
<body>

<h1>Select</h1>
<br>
<form>
<select name="choose">
    <option value="1" selected>One</option>
    <option value="2" >Two</option>
  </select>
  <button name="openlink" onclick="event.preventDefault(); jump();" >Go</button>
</form>

<script type="text/javascript">
  function jump() {
    // arrays start from 0, and used values 1 and 2 in the select
var urls = [ '','[url]https://www.google.com[/url]' , '[url]https://www.linustechtips.com[/url]']
var element = document.querySelector('select[name="choose"]')
var id = parseInt(element.value) // get the value of the select element
console.log(urls[id]) // press F12 to open developer tools and you see it logged there
window.open(urls[id],'_self')  // use _blank to open in new tab
  }
</script>
</body>
</html>
 
The following users thanked this post: firewalker

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3035
  • Country: us
Re: Really simple static html page
« Reply #4 on: October 18, 2022, 03:02:36 pm »
Here's a bare bones solution:

Code: [Select]
<html>
    <select id="the-select">
        <option value="https://eevblog.com">eevblog.com
        <option value="https://example.com">example.com
    </select>
  <button onclick="window.location=document.getElementById('the-select').value">Go to Site</button>
</html>

You can try it out at: https://codepen.io/codeBelt/pen/OVjxJN
 
The following users thanked this post: firewalker

Offline firewalkerTopic starter

  • Super Contributor
  • ***
  • Posts: 2450
  • Country: gr
Re: Really simple static html page
« Reply #5 on: October 18, 2022, 03:07:27 pm »
Here's a bare bones solution:

Code: [Select]
<html>
    <select id="the-select">
        <option value="https://eevblog.com">eevblog.com
        <option value="https://example.com">example.com
    </select>
  <button onclick="window.location=document.getElementById('the-select').value">Go to Site</button>
</html>

You can try it out at: https://codepen.io/codeBelt/pen/OVjxJN

And to open it in a new tab? I must put somewhere target="_blank"?
Become a realist, stay a dreamer.

 

Offline pcprogrammer

  • Super Contributor
  • ***
  • Posts: 3690
  • Country: nl
Re: Really simple static html page
« Reply #6 on: October 18, 2022, 03:20:37 pm »
And to open it in a new tab? I must put somewhere target="_blank"?

Code: [Select]
<html>
    <select id="the-select">
        <option value="https://eevblog.com">eevblog.com
        <option value="https://example.com">example.com
    </select>
  <button onclick="window.open(document.getElementById('the-select').value)">Go to Site</button>
</html>

For that there is window.open

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 3035
  • Country: us
Re: Really simple static html page
« Reply #7 on: October 18, 2022, 03:23:34 pm »
And to open it in a new tab? I must put somewhere target="_blank"?

Sorry, missed that requirement.

Use the same technique that mariush used, i.e. with window.open(..., '_blank'):

Code: [Select]
<button onclick="window.open(document.getElementById('the-select').value,'_blank')">Click me</button>

Here are the options for the second parameter of window.open():

Code: [Select]
_self – It opens the URL in the current tab by replacing the opened page.
_blank – It opens the URL in a new tab. This is the default value.
_parent – It loads the URL into the parent frame. If there is no parent, behave as _self.
_top – It opens the URL by replacing any framesets. If no frameset, behave as _top.

Update: I guess '_blank" is the default so a second parameter isn't necessary.
 

Offline firewalkerTopic starter

  • Super Contributor
  • ***
  • Posts: 2450
  • Country: gr
Re: Really simple static html page
« Reply #8 on: October 18, 2022, 03:25:04 pm »
Thanks you very much!!!

Alexander.
Become a realist, stay a dreamer.

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf