简体   繁体   English

在href末尾附加变量?

[英]Append variable at end of href?

I am trying to generate a twitter stream widget based on the users search. 我正在尝试根据用户搜索生成一个Twitter流小部件。 Using their api, it is easy to make one if the search term is known, such as "corgi". 使用他们的api,如果知道搜索词(例如“ corgi”),就很容易制作一个。 Here is my code: 这是我的代码:

I want to be able to delete "corgi" obviously, and replace it with whatever they search for. 我希望能够明显删除“柯基犬”,并将其替换为他们搜索的任何内容。

EDIT: After some research online, I believe the solution is a bit more complicated that just appending a variable to the href. 编辑:经过一些在线研究后,我认为该解决方案要复杂一些,只是将变量附加到href上。 Now I am using this: 现在我正在使用这个:

$(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.")
  });

    });
    }

);








});
});

The issue is, while everything else in the code works fine, the twttr functions simply appear to do nothing. 问题是,虽然代码中的所有其他内容都正常运行,但twttr函数似乎根本无能为力。 The reason everything is in one function is because I am relying on local storage to store the user input... 一切都在一个功能中的原因是因为我依靠本地存储来存储用户输入...

This should work. 这应该工作。 Just set SUBMIT BUTTON ID to the id of the submit button and SEARCH INPUT ID to the id of the user input 只需将SUBMIT BUTTON ID设置为SUBMIT BUTTON ID ,将SEARCH INPUT ID设置为用户输入的ID

$(SUBMIT BUTTON ID).click(function() {
    var htag = $(SEARCH INPUT ID).val();
    $("twitter-timeline").prop("href", "https://twitter.com/hashtag/"+htag);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM