2017-06-16 27 views
1

我有兩個文件,「a.js」和「b.html」。是否可以將從「a.js」創建的字符串數據附加到「b.html」頭標記?這是我從「a.js」文件中彙編字符串數據的代碼。是否可以將字符串數據從javascript傳遞給html文件?

a.js:

createChart: function() { 
    var pieChartScript1 = 
     '<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>' + 
     '<script type="text/javascript">' + 
     'google.charts.load("current", {packages:["corechart"]});' + 
     'google.charts.setOnLoadCallback(drawChart);' + 
     'function drawChart() {' + 
     'var data = google.visualization.arrayToDataTable(['; 

    var pieChartScript2 = '[' + '0~999' + ', ' + one.length + '],'; 
    pieChartScript2 += '[' + '1000~1999' + ', ' + two.length + '],'; 
    pieChartScript2 += '[' + '2000~2999' + ', ' + three.length + '],'; 
    pieChartScript2 += '[' + '3000~3999' + ', ' + four.length + '],'; 
    pieChartScript2 += '[' + '4000~4999' + ', ' + five.length + '],'; 
    pieChartScript2 += '[' + '5000~' + ', ' + six.length + ']]);'; 

    var pieChartScript3 = 
     'var options = {title: ' + '제목' + ',pieHole: 0.4,};' + 
     'var chart = new google.visualization.PieChart(document.getElementById("donutchart"));' + 
     'chart.draw(data, options);}' + 
     '</script>'; 
    console.log(pieChartScript1 + pieChartScript2 + pieChartScript3); 
    }, 

我希望將其追加在 「b.html」 文件

b.html:

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="UTF-8"> 
 
    <title>Insert title here</title> 
 
</head> 
 
<!--I wish to append it here--> 
 
<body> 
 

 
    <div id="piechart" style="width: 900px; height: 500px;"></div> 
 

 
</body> 
 

 
</html>

+0

你爲什麼在JavaScript裏面編寫JavaScript? – evolutionxbox

+0

我目前正在使用Arcgis webappbuilder小部件,它使用java腳本。之所以我在javascript中編寫javascript是因爲首先,我在編碼方面很糟糕,其次是google提供的在

0

如果你想使用這個js文件,你必須在html文件中使用。我不知道如何運行js字符串作爲你所要求的功能。

+0

中編寫的字符串,這些字符串我希望發送到b.html包含在「var pieChartScript1,2,3」 。你看到的createChart()函數只是一個簡單的函數,用於插入從數據庫中檢索到的值。 – sadilfh

0

如果指定a.js一個全局變量

var myGlobal = "I'm global" 
//rest of script 

在b.html,你將能夠使用myGlobal,如果兩者都通過瀏覽器

0

又一次我被裝笨。我的目標是將字符串數據從js文件發送到另一個html文件。我已經通過使用sessionStorage.setItem()sessionStorage.getItem()解決了這個問題。很簡單,把簡單的字符串變量發送到另一個文件。謝謝你的幫助。 這是關於這方面的更多信息。 https://developer.mozilla.org/ko/docs/Web/API/Window/sessionStorage

相關問題