我有简单的页面用javascript验证输入中写的电子邮件:
email.html:
<!DOCTYPE html>
<html>
<head>
<title>Email validation</title>
<script src="email.js"></script>
</head>
<body>
<span style="padding: 5px;">
<input type="text" id="email-input" placeholder="Email..."></input>
</span>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
email.js:
var checkEmail = function() {
var regexp = /BIG_REGEX/;
var email = document.getElementById('email-input').value;
if (email === '')
removeFrame();
else if (regexp.test(email))
drawFrame('green');
else
drawFrame('red');
};
var removeFrame = function() {
var input = document.getElementById('email-input');
input.parentNode.style.backgroundColor = input.parentNode.parentNode.style.backgroundColor;
};
var drawFrame = function(color) {
var input = document.getElementById('email-input');
input.parentNode.style.backgroundColor = color;
};
window.onload = function() {
document.getElementById('email-input').onkeyup = …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用phantomjs将svg转换为png图像:
var page = require('webpage').create();
page.evaluate(function() {
var svg = '<svg class="tnt_tracks_svg" width="800" height="180" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/\
xlink"><rect width="10" height="10" fill="red"></rect></svg>';
var src = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(svg)));
var img = new Image();
img.src = src;
img.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = img.width;
canvas.height = img.height;
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0);
var src = canvas.toDataURL(); // DOM Exception 18!
}
});
Run Code Online (Sandbox Code Playgroud)
但是在调用toDataURL时我得到了一个DOM Exception 18.我在其他类似的问题中看到,如果svg托管在其他域等中,可能会发生这种情况......,但在我的情况下,我提供了svg src!.上面的代码有什么问题?为什么它会触发DOM Exception?
类似的代码在jsfiddle中看起来不错(没有phantomjs,在chrome和FF中测试过).这个幻影示例也使用fabric.js:
page.includeJs("http://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.4.0/fabric.min.js", function () …Run Code Online (Sandbox Code Playgroud) 我通过npm在我的windows machina上安装了casperjs和phantomjs.但是我得到了这个问题.
C:\>casperjs sample.js
C:\>Unable to open file: sample.js
Unsafe JavaScript attempt to access frame with URL about:blank from frame
with URL file:///C:/Users/vini/AppData/Roaming/npm/node_modules/casperjs/bin/bootstrap.js.
Domains, protocols and ports must match.
Run Code Online (Sandbox Code Playgroud)