在href结尾附加变量?

时间:2016-04-19 21:31:58

标签: javascript jquery html

我正在尝试根据用户搜索生成一个Twitter流小部件。使用他们的api,如果搜索术语已知,则很容易制作一个,例如“corgi”。这是我的代码:

我希望能够显然删除“corgi”,并将其替换为他们搜索的内容。

编辑:在网上进行一些研究之后,我认为解决方案有点复杂,只需将变量附加到href即可。现在我正在使用它:

$(document).ready(function() {

// Upon form submission
$("#formy").submit( function(e) {

    // Stop the <form> from refreshing
    e.preventDefault();

    // Store the user's input
    var id = $('#searchbar').val(); 

    //Make REST call to Twitter API
    $.ajax({
        url: '/search',
        type: 'POST',
        data: {id:id}, 
        dataType: 'text',
        success: function(data) {
            // Redirect to the reults page   
            window.location.href = '/test';
            data = JSON.parse(data);
            //console.log(data);
            // Store data from REST call in local storage
            localStorage.setItem("count", "we analyzed...");
            localStorage.setItem("result1", data.county + " tweets");
            localStorage.setItem("totals", "with a total sentiment score of..");
            localStorage.setItem("result2", data.totalSentiments);
            localStorage.setItem("head",id);
        },

        error: function(jqXHR, textStatus, errorThrown){
            console.log("An error occured.")
            alert(textStatus, errorThrown);
        }
    });
});

// Modify the divs to display the results on the page
jQuery(document).ready(function(){

var id = localStorage.head;
if (localStorage.count) {
    $("#count").text(localStorage.count);
}
if (localStorage.result1) {
    $("#result1").text(localStorage.result1);
}
if (localStorage.totals) {
    $("#totals").text(localStorage.totals);
}
if (localStorage.result2) {
    $("#result2").text(localStorage.result2);
}
if (localStorage.head) {
    $("#head").text("You Searched: " + localStorage.head);
}
localStorage.clear();
//localStorage.removeItem("count");

 window.twttr = (function (d,s,id) {
  console.log("TWITTER  RULES!!!");
      var t, js, fjs = d.getElementsByTagName(s)[0];
      if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
      js.src="https://platform.twitter.com/widgets.js";
      fjs.parentNode.insertBefore(js, fjs);
      return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
  (document, "script", "twitter-wjs");


twttr.ready(function (twttr) {
  console.log("TWITTER  RULES!!!");
    twttr.widgets.createTimeline(
  '721140436522373121',
  document.getElementById('timeline'),
  {
    width: '400',
    height: '440',
    related: localstorage.head
  }).then(function (el) {
    console.log("Embedded a timeline.")
  });

    });
    }

);








});
});

问题是,虽然代码中的其他所有内容都能正常工作,但twttr函数似乎什么都不做。一切都在一个函数中的原因是因为我依靠本地存储来存储用户输入......

1 个答案:

答案 0 :(得分:0)

这应该有效。 只需将this设置为提交按钮的ID,将SUBMIT BUTTON ID设置为用户输入的ID

SEARCH INPUT ID
相关问题