修改我的服务器上的js文件并重新加载它?

发布于 2024-12-03 16:43:58 字数 286 浏览 1 评论 0原文

如果我在

我正在使用 document.write ("") 加载 js 文件,该文件有效。

顺便说一句,这个网站不允许我放置“<”在文档中写。

If I loaded a js file in a <textarea> with a save button below it, can I save that to an existing js file on my server and then reload it. Basically, what I want to do would work just like a wysiwyg except I want to be able to save the js to an existing file.

I am loading the js file using document.write ("<script src='file.js'></script>") which works.

BTW, this site would not let me put the '<' in the document write.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九歌凝 2024-12-10 16:43:58

简短的回答:是的,但从你的帖子来看,我认为你不会觉得这很容易。

更深入地说,您需要使用服务器端语言(例如 PHP)来实现此目的。

我不会介绍如何设置 PHP,我们假设您知道这一点,如果您不知道,那么您就有一个很好的借口去谷歌搜索:) 但是,这里有一个简单的工作示例脚本。

警告:允许人们写入服务器上的文件存在很多陷阱。您当然不想让除了高度信任的人​​之外的任何人都可以访问这样的东西。除非您了解其含义,否则我不能过分强调您对此类事情应该多么小心。

<?php

// Change this to the file you want to be editing
// This path is relative to this script
// So if this script is in /somewhere/something/scripts/save.php
// And your file is at /somewhere/something/files/hello.txt
// This should read: $fname = "../files/hello.txt";
$fname = "something.txt";
// Now don't touch anything else :)



// This checks if we've submitted the file
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // Get the posted content
    $content = $_POST['content'];

    // Open the file, or exit with an error message if we can't
    if (!$handle = fopen($fname, 'w')) {
        echo "Cannot open file ($filename)";
        exit;
    }
    // Write the new content into the file
    fwrite($fhandle, $content);
    fclose($fhandle);
}

// Get the contents of the file
$content = file_get_contents($fname);

?>
<form action="" method="post">
    <p>Editing <?php echo $fname; ?></p>
    <textarea name="content"><?php echo $content; ?></textarea>
    <input type="submit" value="Save" />
</form>

Short answer: Yes, but from your post I don't think you'd find it easy.

In some more depth, you'll need to use a server-side language such as PHP for this.

I won't go into how to get set up with PHP, we'll assume you know that, and if you don't then you have a great excuse to go Googling :) However, here's a simple working example script for you.

WARNING: There are a lot of pitfalls with allowing people to write to files on your server. You CERTAINLY don't want to make something like this accessible to anyone except HIGHLY trusted people. I can't over-emphasise how careful you should be with something like this unless you understand the implications of it.

<?php

// Change this to the file you want to be editing
// This path is relative to this script
// So if this script is in /somewhere/something/scripts/save.php
// And your file is at /somewhere/something/files/hello.txt
// This should read: $fname = "../files/hello.txt";
$fname = "something.txt";
// Now don't touch anything else :)



// This checks if we've submitted the file
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
    // Get the posted content
    $content = $_POST['content'];

    // Open the file, or exit with an error message if we can't
    if (!$handle = fopen($fname, 'w')) {
        echo "Cannot open file ($filename)";
        exit;
    }
    // Write the new content into the file
    fwrite($fhandle, $content);
    fclose($fhandle);
}

// Get the contents of the file
$content = file_get_contents($fname);

?>
<form action="" method="post">
    <p>Editing <?php echo $fname; ?></p>
    <textarea name="content"><?php echo $content; ?></textarea>
    <input type="submit" value="Save" />
</form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文