onclick在轨道4上更改父url ruby

时间:2016-12-25 09:30:48

标签: javascript ruby-on-rails

目标:点击打开新标签页并更改父页面的网址。

  <%= search_form_for @q, url: pages_auto_path do |f| %>
      <%= f.search_field(:name) %>
      <%= f.button :submit, :id => "changeit", :formtarget => "_blank" do %>
         Search
      <% end %>
  <% end %>

application.js文件

$("#changeit").on("click", function(){
    window.location.replace("http://www.google.com");
});

点击搜索按钮后,新标签打开但父页面保持不变。我需要父页面来重定向其他网址。

任何想法,这里有什么问题?

2 个答案:

答案 0 :(得分:0)

你可以尝试一下

window.opener.location.href="https://google.com"

所以你可以尝试一下

$("#changeit").on("click", function(){
    setTimeout(function() { location.href='http://google.com'; }, 0) }
}

答案 1 :(得分:0)

你试试这个..

import urllib2,re,urllib,urlparse,csv,sys,time,threading,codecs,shutil
from bs4 import BeautifulSoup


def extract(url):
    try:
        sys.stdout.write('0')
        # global file
        page = urllib2.urlopen(url).read()

        soup = BeautifulSoup(page, 'html.parser')

        product = soup.find("div", {"class": "js-product-price"})
        price = product.findNext('div',{'class': 'js-price-display'}).getText().strip()
        oos = product.findNext('p', attrs={'class': "price-oos"})

        if oos is None:
            oos = 'In Stock'
        else:
            oos = oos.getText()

        val = url + "," + price + "," + oos + "," + time.ctime() + '\n'
        # ifile.write(val)
        sys.stdout.write('1')
    except Exception as e:
        print e

    else:
        return val

while True:
    ifile = open('output.csv', "w", 0)
    inputs = csv.reader(open('input.csv'))
    # inputs = csv.reader(codecs.open('input.csv', 'rU', 'utf-16'))

    ifile.write('URL' + "," + 'Price' + "," + 'Stock' + "," + "Time" + '\n')

    for i in inputs:
        val_to_write = extract(i[0])
        if val_to_write:
            ifile.write(val_to_write)
        ifile.close()

    shutil.copy('output.csv', 'output2.csv')

print("finished")

否则试试这个..

//As an HTTP redirect (back button will not work )
window.location.replace("http://www.google.com");

//like if you click on a link (it will be saved in the session history, 
//so the back button will work as expected)
window.location.href = "http://www.google.com";