Editing a javascript document.write(variable)

Editing a javascript document.write(variable)

本文关键字:variable write document javascript Editing      更新时间:2023-09-26

>我有一个JS函数

function setComparison(data) {
    var w= window.open('', 'comparison', 'width=600, height=400');
    w.document.open();
    w.document.write(getComparisonContent(data));
    w.document.close();
    return false;
}

我无权访问创建(数据)的代码,但我需要替换(数据)中的字符串。有没有办法通过字符串替换方法通过管道(数据)?

data.replace()似乎是你最好的选择......

function setComparison(data) {
    var w= window.open('', 'comparison', 'width=600, height=400');
    w.document.open();
    data = data.replace("foo", "bar");
    w.document.write(getComparisonContent(data));
    w.document.close();
    return false;
}

在这里,foo将被替换为bar

之前的答案对我不起作用,但这确实有效:

function setComparison(data) {
    var w= window.open('', 'comparison', 'width=600, height=400');
    w.document.open();
    html = html.replace("string to replace", "replacement text");
    w.document.write(getComparisonContent(data));
    w.document.close();
    return false;
}