javascript - Append variable at end of href? -
i trying generate twitter stream widget based on users search. using api, easy make 1 if search term known, such "corgi". here code:
i want able delete "corgi" obviously, , replace whatever search for.
edit: after research online, believe solution bit more complicated appending variable href. using this:
$(document).ready(function() { // upon form submission $("#formy").submit( function(e) { // stop <form> refreshing e.preventdefault(); // store user's input var id = $('#searchbar').val(); //make rest call twitter api $.ajax({ url: '/search', type: 'post', data: {id:id}, datatype: 'text', success: function(data) { // redirect reults page window.location.href = '/test'; data = json.parse(data); //console.log(data); // store data rest call in local storage localstorage.setitem("count", "we analyzed..."); localstorage.setitem("result1", data.county + " tweets"); localstorage.setitem("totals", "with 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 divs display results on 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 timeline.") }); }); } ); }); });
the issue is, while else in code works fine, twttr functions appear nothing. reason in 1 function because relying on local storage store user input...
this should work. set submit button id
id of submit button , search input id
id of user input
$(submit button id).click(function() { var htag = $(search input id).val(); $("twitter-timeline").prop("href", "https://twitter.com/hashtag/"+htag); });
Comments
Post a Comment