简体   繁体   English

修改js文件的内容

[英]modify contents of a js file

Hi I have a javascript array of Url's in content.js file. 嗨,我在content.js文件中有一个Url的javascript数组。 I need to display the urls in seperate input boxes on a seperate page say display.php. 我需要在单独的页面上将URL显示在单独的页面上,例如display.php。 Edit and delete buttons are given along with each input boxes. 每个输入框都提供了编辑和删除按钮。 any change made by the user should be reflected in the content.js file. 用户所做的任何更改都应反映在content.js文件中。

I have shown all the url's in the input boxes. 我已经在输入框中显示了所有网址。 now an onclick event calls a function on button press. 现在,onclick事件会在按下按钮时调用一个函数。

My question is what will be the best way to update the array in content.js file(the file contains only one array). 我的问题是更新content.js文件中的数组的最佳方法是什么(该文件仅包含一个数组)。 I'm trying to use file handling but that will require the content.js to have a strict format always. 我正在尝试使用文件处理,但这将要求content.js始终具有严格的格式。 Is there a better way to modify the contents of the array or delete rows from array without file handling? 有没有更好的方法来修改数组的内容或从数组中删除行而不进行文件处理?

In first step, you should read the contents of JS file and pass it to json_decode function. 第一步,您应该阅读JS文件的内容并将其传递给json_decode函数。 It will give you PHP array. 它将为您提供PHP数组。 Use this array to display text boxes to user. 使用此数组向用户显示文本框。

In second step, you should get the strings the user was submitted (eg from $_POST ) and construct a PHP array from them. 在第二步中,您应该获取用户提交的字符串(例如,从$_POST ),并从中构造一个PHP数组。 Then use json_encode to convert PHP array to JavaScript array represented in a string. 然后使用json_encode将PHP数组转换为以字符串表示的JavaScript数组。 Then you can write this string to JS file. 然后,您可以将此字符串写入JS文件。 So its content will be always valid. 因此其内容将始终有效。

You could do it with a server-side language like PHP. 您可以使用服务器端语言(例如PHP)来实现。 See this tutorial on how to write files using PHP. 请参阅本教程 ,了解如何使用PHP编写文件。 I would convert the array into a string using json_encode and use json_decode to convert it back into an array after reading it . 我将使用json_encode将数组转换为字符串,并在读取后使用json_decode将其转换回数组。

If you're only saving these URLs for the user and nobody else (so only the person entering/editing the data needs to see it) then I recommend using HTML5 localStorage 如果您只为用户保存这些URL,而没有其他人保存(因此只有输入/编辑数据的人员需要查看它们),那么我建议使用HTML5 localStorage

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

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