python
python

Reputation: 31

How to redirect a page amp?

I need to adapt the following javascript code to an amp page.

<select id="url"> 
   <option value='home'>home</option> 
   <option value='contact'>contact</option> 
</select>

<input type="button" value='ir' onclick='ir()'> 

<script> 
function ir(){ 
   var url=document.getElementById("url").value; 
   location.href=url; 
}
</script>

It's possible?

In the documentation I did not find the answer.

Upvotes: 1

Views: 7832

Answers (1)

Bachcha Singh
Bachcha Singh

Reputation: 3944

You are using button and onclick function, in place of this in AMP you can use anchor tag <a href="Value">Value</a> and change the url using `amp-bind'

You can use amp-bind to achieve your goal

Here is working URL

add JS in header

<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>

Add code in body

<div class="wrapper">

    <select on="change:AMP.setState({ url: event.value })">
      <option value='home'>home</option> 
      <option value='contact'>contact</option> 
    </select>

    <a [href]="url" href="home">ir</a>

</div>

Updated - if some one want to redirect on option change than use navigateTo action

Example :

<select on="change:AMP.navigateTo(url=event.value)">
    <option value="http://google.com">google.com</option>
    <option value="http://yahoo.com">yahoo.com</option>
    <option value="http://bing.com">bing.com</option>
  </select>

Upvotes: 5

Related Questions