2013-10-29 119 views
3

我目前使用此代码:外部JavaScript文件提取?

var wordRandomizer = { 
    run: function (targetElem) { 
     var markup = this.createMarkup(); 
     targetElem.appendChild(markup); 
    }, 
    createMarkup: function() { 
     var that = this; 
     var frag = document.createDocumentFragment(); 
     this.elem = document.createElement('span'); 
     var button = document.createElement('button'); 
     button.innerText = 'Change Item'; 
     button.addEventListener('click', function() { 
      that.changeItem(); 
     }); 
     frag.appendChild(this.elem); 
     frag.appendChild(button); 
     return frag; 
    }, 
    changeItem: function() { 
     var rand = this.getRandInt(1, this.items.length) - 1; 
     console.log(rand); 
     this.elem.innerText = this.items[rand]; 
    }, 
    getRandInt: function (min, max) { 
     return Math.floor(Math.random() * (max - min + 1)) + min; 
    }, 
    items: ['itemA', 'itemB', 'itemC', 'itemD'] 
}; 
wordRandomizer.run(document.body); 

我的代码是压在争夺列表中的项目之一,当一个按钮。但是,我不希望这些项目与生成器显示在同一页面上,因为人们只是看源代码。我该如何做到这一点,所以一旦按下按钮,它会从另一个位置抓取随机项目,而人们无法使用源代码查看它们。

如果有帮助,你可以在这里看到了正在运行的代码 - http://jsbin.com/ESOdELU/1/edit

+5

如果你真的需要保留这个名单了用户手中的,你需要一些服务器端语言 - 并不重要(ASP.NET,PHP,导轨等)。如果您试图使用静态HTML/JS或纯文本数据源完成此操作,您的用户将能够看到列表。 –

+0

为此,您将不得不使用一些服务器端编程和ajax来使用它。 – DontVoteMeDown

+0

如果这在浏览器中运行,那么用户可以看到脚本可以看到的所有内容。 – Joe

回答

0

我会使用PHP,因为它是一个免费的脚本语言,是最有可能由主机或默认支持给你解决Web服务器...

对于初学者来说,这里是代码,包括jQuery和基本AJAX脚本

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 

<script type="text/JavaScript"> 
$(document).ready(function(){ 
    $("#generate").click(function(){ 
    $("#madlibs p").load("script.php"); 
    }); 
}); 
</script> 

这里是的script.php

012码
<?php 
header("Cache-Control: no-cache"); 
// For testing you can use an inline array like the lines below 
// Just remove the comment slashes "//" from the beginning of the line 
// and comment out the external declarations 

//$actors = array('Denzel Washington','Michael J. Fox','Jim Carey','Boris Kodjoe'); 
//$roles = array('Mental Patient','Homeless Musician','Drag Queen Detective','Tormented Mathematician'); 

// In production, you would put these in a text file or a database. 
// For $actors, put an entry on each line of a text file and save it as 'leads.txt' 
// Do the same with a separate file for $roles (roles.txt). 
$actors = file("leads.txt"); 
$roles = file("roles.txt"); 

// This selects a random element of each array on the fly 
echo $prefixes[rand(0,count($actors)-1)] . " stars as a " 
    . $suffixes[rand(0,count($roles)-1)] . " in the next blockbuster film."; 
// Example output: 
// Michael J. Fox stars as a Tormented Mathematician in the next blockbuster film. 
?> 

将它放在页面的主体中,并确保将所有样式都显示出来。

<body> 
    <div id="madlibs"><p> </p></div> 
    <button id="generate">Surprise Me!</button> 
</body> 

有两点要注意: - 您可以在该文件的script.php的基本布局HTML,然后就只需要DIV的ID中,你会显示结果$(“# madlibs“)

  • 您可以使用任何服务器端语言来达到相同的结果,只是交换了外部文件调用相应的名和扩展名(.asp的,.CFM等)

这里是一个链接到原始教程,帮助我与类似的项目: http://www.sitepoint.com/ajax-jquery/

我希望这可以帮助。对不起,我在午餐时无法提供纯Java的JavaScript解决方案。