首页 \ 问答 \ 寻找一些js范围的黑客(Looking for some js scope hack)

寻找一些js范围的黑客(Looking for some js scope hack)

这里有一些你可能或可能不知道的javascript函数参数变量的有趣行为:

function foo(bar) {
    console.log('bar was:', bar); 
    arguments[0] = 'zap';        
    console.log('bar now:', bar); 
}  

foo('bam'); 
// bar was: bam
// bar now: zap

正如您所见, bar变量现在指向另一个值。

我想以一种稍微奇怪的方式利用这种行为,我想知道是否有可能以某种方式从函数范围外部改变参数值?

也许使用call / apply或其他一些trycky js功能?

所以我可以在调用后更改函数参数的值,例如:

function chooseNumber(number) { 
    setInterval(function() { 
        console.log('I choosed:', number) 
    }, 1000) 
}

chooseNumber(1);
// I choosed: 1
// I choosed: 1
// I choosed: 1
// ...

然后,如果我决定改变主意,我怎样才能输出初始函数:

// I choosed: 2
// I choosed: 2
// I choosed: 2
// ...

Here some interesting behaviour of javascript function arguments variable that you may be or maybe not aware of:

function foo(bar) {
    console.log('bar was:', bar); 
    arguments[0] = 'zap';        
    console.log('bar now:', bar); 
}  

foo('bam'); 
// bar was: bam
// bar now: zap

As you can see bar variable now pointing out to another value.

I want to make use of such behavior in a slightly strange way, I want to know is it possible to change argument value from the outside of the function scope somehow?

Maybe using call/apply, or some other trycky js features?

So I can change the value of function argument after it was called, for example:

function chooseNumber(number) { 
    setInterval(function() { 
        console.log('I choosed:', number) 
    }, 1000) 
}

chooseNumber(1);
// I choosed: 1
// I choosed: 1
// I choosed: 1
// ...

Then if I dicided to change my mind, how can I make so that initial function would output:

// I choosed: 2
// I choosed: 2
// I choosed: 2
// ...

原文:https://stackoverflow.com/questions/18290276
更新时间:2023-10-22 22:10

最满意答案

尝试这个:

<%= f.submit :onclick => "return confirm('Are you really sure you want to use '+
          document.getElementById('number_of_bars_id').value + '?' )" %>

number_of_bars_id替换为您的字段的ID。

如果你稍微谷歌,你会发现一种方法可以使用:confirm


Try this:

<%= f.submit :onclick => "return confirm('Are you really sure you want to use '+
          document.getElementById('number_of_bars_id').value + '?' )" %>

Replace the number_of_bars_id with the id of your field.

If you google a bit you will find a way for this to work with :confirm.

相关问答

更多
  • 您可以通过action=file1.php和action=file2.php将表单发送到不同的处理程序。 他们是否使用一堆相同的代码进行处理? 将其放入单独的文件中,包含共同点,并在每个处理文件中写入唯一的位。 不要破解,组织。 对于Javascript验证,不要停止默认操作,然后继续,而是这样做: if (validation != valid) { return false; } 这样,如果JS关闭或验证失败,表单动作/事件是完好的,它的行为如预期的,否则它bonks。 当然,肯定会保留服务器 ...
  • 来自OP的评论: echo "onclick='return confirm(\'Are you sure you want to submit this form?\');'" 您不能在用单引号分隔的属性值中使用单引号。 你的选择是: 在JS中使用双引号 echo "onclick='return confirm(\"Are you sure you want to submit this form?\");'" 在HTML中使用双引号 echo "onclick=\"return confirm(' ...
  • 在您的控制器中,您可以访问request对象。 这有一个由referrer名称的属性。 request.referrer将为您提供请求来自的网站。 Im your controller you have access to a request object. This has a attribute by the name of referrer. request.referrer will get you the site the request has come from.
  • 这样的事怎么样? 我们的想法是在提交事件上捕获(并触发!),而不是点击事件。 $(document).ready(function () { $('#myForm').submit(function() { var f = this; confirm("Really delete this event?", function () { $(f).submit(); }); return false; // ca ...
  • 对不起,你必须使用JS(确认也是Rails提供的JS功能)。 如果您需要有关JS实现的更多帮助,您应该提供更多信息(和代码)。 I'm sorry, but you have to use JS (the confirmation is also a JS function provided by Rails). You should give more info (and code) if you need more help about the JS implementation.
  • 你可以添加changed? 检查你的模板: <% if @post.changed? && @post.invalid? %>
    <% @post.errors.full_messages.each do |message| %> <%= message %>
    <% end %>
    <% end %> You may add changed? check to your template ...
  • 每次载入主要内容时,似乎都会载入JavaScript。 因此每次更改事件都与表单绑定。每次加载主内容时都需要删除以前的绑定。 在$('body').on('submit', 'form', function(e){ $(body).off("submit", "form"); 它会关闭以前的绑定并保持一个绑定存活。 Each time you load the main content it seems you javascript also loading with it. So change even ...
  • 当我将代码替换为现有simple_form代码上的按钮时,它似乎有效。 嗯你能发布simple_form的代码吗? So it turns out this was a problem with the jquery-rails gem. https://github.com/rails/jquery-rails/issues/173 In my gem file, I changed from this: gem 'jquery-rails' To this: gem 'jquery-rails', '~ ...
  • 你快到了。 提交的表单由创建操作处理,而不是由新操作处理。 你有正确处理html或js的语法 - 你只需要在你的创建动作中使用它,以及一个包含你要渲染的js的create.js.erb。 你的PostsController应该有一个创建函数 def create @post = Post.new(params[:post]) respond_to do |format| if @post.save format.html format.js else ...
  • 尝试这个: <%= f.submit :onclick => "return confirm('Are you really sure you want to use '+ document.getElementById('number_of_bars_id').value + '?' )" %> 将number_of_bars_id替换为您的字段的ID。 如果你稍微谷歌,你会发现一种方法可以使用:confirm 。 Try this: <%= f.submit :onclick => ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)