[英]How do I add the parameter of a javascript function into a url?
Say I have a function: 说我有一个功能:
function potato(param) {
document.body.style.backgroundImage = 'url(file:///C:/Users/Joe)';}
and I want to make it so when I call potato('/chicken.jpg'), it will modify the url in potato to be file:///C:/Users/Joe/chicken.jpg. 当我打电话给马铃薯('/ chicken.jpg')时,我想这样做,它会将马铃薯中的url修改为file:/// C:/Users/Joe/chicken.jpg。 How could this be done?
怎么可以这样做? Thank you
谢谢
Javascript in a browser doesn't have access to the file system. 浏览器中的Javascript无权访问文件系统。 Try running a local web browser with a document root of C:\\User\\Joe and then pass in the url path ... something like ' http://localhost/chicken.jpg '
尝试使用文档根目录C:\\ User \\ Joe运行本地Web浏览器,然后传入url路径...类似' http://localhost/chicken.jpg '
Just use concatenate operator. 只需使用concatenate运算符。 So in JavaScript use +.
所以在JavaScript中使用+。
'my/base/url' + myStringVar; 'my / base / url'+ myStringVar;
'url(file:///C:/Users/Joe'+param+')'; 'URL(文件:/// C:/用户/乔' + PARAM + ')';
I think you want something like this. 我想你想要这样的东西。 This code will replace the background image when you call the function with the param :
使用param调用函数时,此代码将替换背景图像:
function potato(param){
var $element = document.querySelector('.something'),
url = 'file:///C:/Users/Joe/' + param;
$element.style.backgroundImage = "url('" + url + "')";
console.log('=>' + $element.style.backgroundImage);
}
This is live example on jsbin => https://jsbin.com/?html,console,output 这是jsbin => https://jsbin.com/?html,console,output上的实例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.