2016-04-19 20 views
0

私はユーザーの検索に基づいて、Twitterのストリームウィジェットを生成しようとしています。 apiを使って、 "corgi"のように検索語が分かっていれば簡単に作成できます。ここに私のコードです:hrefの最後に変数を追加しますか?

私は明らかに "corgi"を削除し、検索するものに置き換えることができます。

EDIT:オンラインでの調査の結果、ソリューションは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つの機能に含まれているのは、ローカルストレージに依存してユーザーの入力を保存するためです。

+1

アウトできますが、スペースあなたのコードとそれをインデントのidをsubmitボタンのidとSEARCH INPUT IDSUBMIT BUTTON IDを設定しますか?コードが読み込み可能であれば、何が間違っているかを見るのがずっと簡単です。 –

+0

注:['location.protocol'](https://developer.mozilla.org/en-US/docs/Web/API/HTMLHyperlinkElementUtils/protocol)は、文字列' 'http:" 'または' " https: "あなたがそこにある正規表現テストの必要はありません。 – Phrogz

+0

スクリプトコードは、あなたのタイムラインを生成するときにtwitterが提供するものです –

答えて

0

これは動作するはずです。 ただ、ユーザーの入力

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

@Adraxas_Nazaron私はそれがうまくいくと思ったがエラーが出る。 JSONの位置0に予期せぬトークンuがあります。 –

+0

私はあなたのコードをどこに置くべきだと思いますか? main.jsか上のスクリプトタグ内にありますか? –

+0

それはあなたの入力をどのように受け取っているのでしょうか –

関連する問題