JavaScript 在浏览器中的相关概念:执行环境、引入脚本、安全策略等

The Window as Global Execution Context
窗口作为全局执行环境

[quote]document property for the Document object
two self-referential properties, window and self

a location property that refers to the Location object associated with the window. When a web browser displays a framed document

the frames[] array of the top-level Window object contains references to the Window objects that represent the frames

The Window object represents a web browser window (or a frame within a window; in client-side JavaScript, top-level windows and frames are essentially equivalent).
[/quote]

document属性引用Document对象

location属性引用与该window相关的Location对象

frames[]数组保存了文档中表单元素的引用

The Client-Side Object Hierarchy and the DOM
DOM文档树

[img]/upload/attachment/126729/26f9bbde-577b-3c1f-b694-c700e2d0c1a9.png[/img]


The Role of JavaScript on the Web
JS在浏览器中的角色

[quote]Web browsers display HTML-structured text styled with CSS stylesheets. HTML defines the content, and CSS supplies the presentation. Properly used, JavaScript adds behavior to the content and its presentation. The user's experience should not be dependent on JavaScript, but JavaScript can serve to facilitate that experience. [/quote]

为内容和表现进一步添加行为

Unobtrusive JavaScript
不唐突的JS

[quote]
As its name implies, this paradigm holds that JavaScript should not draw attention to itself; it should not obtrude.

The first goal of unobtrusive JavaScript is to keep JavaScript code separate from HTML markup. As a corollary to this goal, you should strive to make your external files of JavaScript code as modular as possible.

The second goal of unobtrusive JavaScript is that it must degrade gracefully.

A third goal of unobtrusive JavaScript is that it must not degrade the accessibility of an HTML page (and ideally it should enhance accessibility).[/quote]

尽量不凸显JS的存在

首先,与HTML代码分离
其次,平稳退化
再次,增强可用性;也就是稳步增强

Embedding Scripts in HTML
在HTML中嵌入代码
[quote]Between a pair of <script> and </script> tags
From an external file specified by the src attribute of a <script> tag
In an event handler, specified as the value of an HTML attribute such as onclick or onmouseover
In a URL that uses the special javascript: protocol[/quote]

<script>标签之间
<scirpt>引入的单独JS文件
HTML的事件属性中的代码
javascirpt伪协议所引用的代码


注意CDATA标记

<script><![CDATA[// Your JavaScript code goes here
]]></script>


The defer Attribute
defer属性

[quote]As mentioned earlier, a script may call the document.write( ) method to dynamically add content to a document. Because of this, when the HTML parser encounters a script, it must normally stop parsing the document and wait for the script to execute. [/quote]


改变<script>代码块的执行顺序
可以延迟该块的执行,继续HTML解析

Event Handlers in HTML
事件句柄
[quote]
onclick
This handler is supported by all button-like form elements, as well as <a> and <area> tags.

onmousedown, onmouseup
These two event handlers are a lot like onclick, but they are triggered separately when the user presses and releases a mouse button, respectively. Most document elements support these handlers.

onmouseover, onmouseout

onchange
This event handler is supported by the <input>, <select>, and <textarea> elements. It is triggered when the user changes the value displayed by the element and then tabs or otherwise moves focus out of the element.

onload
This event handler may appear on the <body> tag and is triggered when the document and its external content (such as images) are fully loaded. [/quote]

在执行js伪协议的语句是,确保返回值为空,否则会刷新当前页面来显示

The onload Event Handler
onload事件句柄

这个是在文档完全解析完并且所有资源都下载后执行的,这里不再允许执行document.write()。很显然这里文档已经渲染完毕,如果再有document.write(),那么浏览器就会新建一个空白文档来写入内容。用户就没有机会看到之前文档的内容了。

Client-Side Compatibility
客户端兼容性

目前保证兼容性最好的方法不是判断浏览器再为他们编写各自的代码,而是进行功能性判断。

Feature Testing
功能测试
[quote]
Feature testing (sometimes called capability testing) is a powerful technique for coping with incompatibilities.[/quote]
简单来说也就是判断某一个函数或方法在特定的环境下是否存在,并根据结果进行对应操作。而不是盲目地根据不可靠的方法判断浏览器种类和版本。

例如下面对事件模型的判断:

if (element.addEventListener) { // Test for this W3C method before using it
element.addEventListener("keydown", handler, false);
element.addEventListener("keypress", handler, false);
}
else if (element.attachEvent) { // Test for this IE method before using it
element.attachEvent("onkeydown", handler);
element.attachEvent("onkeypress", handler);
}
else { // Otherwise, fall back on a universally supported technique
element.onkeydown = element.onkeypress = handler;
}


还有一种方法就是尝试所有可能的函数,直到有一种得出了正确的结果;往往这个是由于不同浏览器实现同一种操作的函数名称不一样。

例如下面这个新建XHR对象的例子:

// This is a list of XMLHttpRequest creation functions to try
HTTP._factories = [
function( ) { return new XMLHttpRequest( ); },
function( ) { return new ActiveXObject("Msxml2.XMLHTTP"); },
function( ) { return new ActiveXObject("Microsoft.XMLHTTP"); }
];

// When we find a factory that works, store it here
HTTP._factory = null;

// Create and return a new XMLHttpRequest object.
//
// The first time we're called, try the list of factory functions until
// we find one that returns a nonnull value and does not throw an
// exception. Once we find a working factory, remember it for later use.
HTTP.newRequest = function( ) { /* fuction body omitted */ }



JavaScript Security
JS的安全策略

[quote]A JavaScript program can open new browser windows, but, to prevent pop-up abuse by advertisers, many browsers restrict this feature so that it can happen only in response to a user-initiated event such as a mouse click.

A JavaScript program can close browser windows that it opened itself, but it is not allowed to close other windows without user confirmation. This prevents malicious scripts from calling self.close( ) to close the user's browsing window, thereby causing the program to exit.

A JavaScript program cannot obscure the destination of a link by setting the status line text when the mouse moves over the link.

A script cannot open a window that is too small (typically smaller than 100 pixels on a side) or shrink a window too small.

The value property of HTML FileUpload elements cannot be set.

A script cannot read the content of documents loaded from different servers than the document that contains the script. Similarly, a script cannot register event listeners on documents from different servers.
[/quote]

JS可以打开浏览器窗口,但是大多数浏览器都会限制这个功能。由于广告代码的干扰,浏览器通常只让用户点击行为中的打开窗口代码生效。

JS也不允许未经用户允许关闭浏览器窗口。

JS也不允许在鼠标滑过链接的时候通过改变状态栏的提示文字来混淆浏览者。大多数浏览器都禁止更改状态栏文本了。

JS也不允许把窗口调节至某一个边长小于100像素。

FileUpload组件的value值不能够被更改。

对于不同服务器上的页面,脚本既不能读取其内容,也不能注册事件。

The Same-Origin Policy
同源策略

[quote]Specifically, a script can read only the properties of windows and documents that have the same origin as the document that contains the script

To support large web sites of this sort, you can use the domain property of the Document object. By default, the domain property contains the hostname of the server from which the document was loaded. You can set this property, but only to a string that is a valid domain suffix of itself.[/quote]

这个可以看做是上面最后一条的详细解释。
尽管脚本会限制不同服务器的文档,但是现在的大型站点往往有许多子域名。在JS的安全特性限制下,这些子域名下的脚本也是无法通讯的。好在有一个document.domain属性。

将需要互相通讯的文档的document.domain设置为同一个顶级域名就可以突破这个限制。

例如:
img.aaa.com的页面中document.domain设置为aaa.com;
serach.aaa.com的页面中document.domain同样设置为aaa.com;
浏览器中的扩展和油猴脚本有什么区别? 浏览器中的扩展和油猴脚本有什么区别?脚本和油猴又是什么关系呢? 阅读详情

相关推荐

浏览器中的安全策略——系统安全、网络安全、页面安全

浏览器中的安全,可以按照系统安全、网络安全、页面安全三个方面来了解。

wuli1024的博客 1322

chrome 您的浏览器禁用了Javascript

解决方法如下: 点击浏览器右上方三个小点 - settings - 安全和隐私设置 - 网站设置 - Javascipt - 开启 相关链接

jerry的博客 7259

【网络安全 | 浏览器安全机制】内容安全策略(CSP)及沙箱环境

为了防范 XSS 攻击,许多网站不得不在多个细节上采取安全措施,从而降低了开发效率。对此,业界提出了一种更根本的解决方案:通过浏览器自动禁止外部注入恶意脚本。这正是“内容安全策略”(Content Security Policy,简称 CSP)的由来。内容安全策略(Content Security Policy,CSP)是一种Web安全机制,用于帮助防范和减轻特定类型的攻击,包括跨站脚本攻击(Cross-Site Scripting,XSS)和数据注入等。它通过允许网站管理员定义和实施一系列安全策略

等风来 9793

three.js如何在本地开启服务器

如何在本地运行three.js从外部文件载入的内容运行一个本地的服务器Node.js serverPython serverRuby serverPHP serverLighttpdIIS 作者:大脸猫 更多精彩文章微信搜索【砖人专语】 倘若你只是使用Three.js库中所提供的几何体,且不载入任何纹理贴图,则网页是可以从本地的文件系统中打开,并且是能够直接运行的,只需在文件管理器中双击HTML...

知屋安砖 2790

JavaScript在IE中打开本地文件的实现与安全策略

互联网浏览器安全区域(IE Security Zones)是一种基于信任级别的安全机制,它允许用户根据不同的来源将网页分成不同的安全类别。通过这种方式,可以对每个区域施加不同级别的安全限制,以避免潜在的安全风险。IE安全区域的作用在于对网页内容的执行和访问权限进行控制,尤其当内容涉及到ActiveX控件、脚本以及文件系统访问等敏感操作时。ActiveXObject是Internet Explorer用于创建ActiveX对象的内置JavaScript对象。

weixin_35706255的博客 435

Redis Lua脚本与Node.js安全策略的深度解析

本文深入探讨了Redis Lua脚本的使用方法及其在Node.js应用中的性能优势和潜在风险。同时,文章还涉及了Node.js应用的安全策略,包括如何识别和管理应用程序的攻击面,以及如何维护和升级Node.js项目以确保安全性。

weixin_42510201的博客 399

javascript的安全策略

1、受限制的特性,javascript失去了某些能力。即它本身不支持某些功能。例如:客户端javascript没有提供在客户计算机上的写文件、删文件;创建目录删除目录等。没有File对象,这样不能再用户系统中种植病毒; 同样,它还没有任何的联网原语,javascript可以加载URL,但不能同web上的其他主机建立连接,这样避免了客户机作为攻击平台 2、同源策略 同源策略,即一...

gb5332360的专栏 321

CAN基础知识

CAN基础知识

shouchen1的博客 9150

js浏览器环境

BOM 是一个用于访问浏览器和计算机屏幕的对象集合。通过全局对象window来访问这些对象。 window.navigator 用于反映浏览器及其功能信息的对象 window.navigator.userAgent; 用于浏览器识别的长字符串 window.navigator.userAgent; "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv...

www.小白 901

Android GWP-Asan机制详解

基本用途:GWP-ASan是一种原生内存分配器功能,在内存分配时额外记录一些信息,用于检测DOUBLE_FREE、USE_AFTER_FREE、INVALID_FREE、BUFFER_OVERFLOW、BUFFER_UNDERFLOW五种内存错误问题。

专注于系统快稳省领域 4145

避免踩坑!杰理AC63蓝牙名修改常见问题及解决方案(含BLE后缀处理)

本文详细解析了杰理AC63蓝牙设备名称修改的全过程,包括基础操作、BLE后缀处理及高级调试技巧。针对蓝牙名修改中的常见问题,提供了实用的解决方案和代码示例,帮助开发者避免踩坑并实现高效定制。

q6r7s8t9的博客 413

内容安全策略(CSP)详解

在上一篇博客我们主要总结了XSS攻击,本篇博客主要总结内容安全策略CSP的相关内容。 一、定义 内容安全策略(CSP),是一种安全策略,其原理是当浏览器请求某一个网站时,告诉该浏览器申明文件可以执行,什么不可以执行。CSP是专门解决XSS攻击而生的神器。 CSP的引入会使得我们的引入扩展程序更加安全,并且可以由开发者指定可以加载扩展程序的类型,避免恶意的脚本在浏览器中执行,造成信息泄露问题。 二、CSP的意义 CSP是防XSS的利器,可以把其理解为白名单,开发者通过设置CSP的内容,来规定浏览器可以加载的资

weixin_47450807的博客 9027

Synopsys工具filter命令:从数据筛选到高效IC设计的实战指南

在数字集成电路设计流程中,数据筛选与查询是工程师处理海量网表、时序报告和设计约束的核心需求。其原理在于通过声明式查询语言,直接对设计对象(如单元、网络、路径)的属性进行逻辑判断与模式匹配,从而精准定位目标。这种技术价值在于将工程师从繁琐的过程式脚本循环中解放出来,大幅提升代码可读性与运行效率,直接调用工具内置的高性能查询引擎。在实际应用场景中,它广泛用于物理与时序联合分析、层次化模块操作、动态约束生成以及自动化报告生成。本文聚焦于Synopsys工具链中filter命令的高阶用法,深入解析其表达式语法、正则

weixin_28007513的博客 577

Shortkeys浏览器插件中JavaScript执行的安全策略问题解析

Shortkeys是一款流行的浏览器快捷键扩展工具,它允许用户自定义快捷键来执行各种操作,包括运行自定义JavaScript代码。然而,随着现代浏览器安全策略的日益严格,Shortkeys在执行内联JavaScript时遇到了内容安全策略(CSP)的限制。 ## 问题本质 现代浏览器实施了严格的内容安全策略(CSP),旨在防止跨站脚本攻击(XSS)。当Shortkeys尝试在受CSP保护的页面...

gitblog_07057的博客 418

从FPGA/ASIC工程师视角看DDR:手把手拆解Memory Controller、PHY与DRAM芯片的协作流程

本文从FPGA/ASIC工程师视角深入解析DDR内存子系统,详细拆解Memory Controller、PHY与DRAM芯片的协作流程。通过系统架构分解、接口标准解析和实战调试技巧,帮助工程师优化DDR性能并解决常见问题,涵盖地址映射、命令调度、时序校准等关键技术要点。

weixin_42548888的博客 458

【YOLOv11多模态涨点改进】独家创新首发| TGRS 2026 |引入 CIFusion 通道交互融合模块,通过跨特征交互机制强化目标区域响应,适合多模态融合目标检测,小目标检测高效涨点

🔥本文提出了一种改进YOLOv11多模态目标检测模型的方法,通过引入CIFusion通道交互融合模块来提升RGB与红外特征融合效果。CIF模块采用通道级自适应交互机制,根据全局上下文动态分配不同模态通道的重要性,实现跨模态特征交互与互补信息融合。相比传统拼接或加权融合方式,CIF能够增强目标区域响应,抑制背景噪声,提高小目标和低对比度目标的检测性能。实验表明,该方法在DroneVehicle等数据集上显著提升了检测精度。论文还详细介绍了CIF模块的结构原理、实现代码及模型配置方法。

Ai缝合怪、助力小伙伴高效发论文 187

Flask 应用中执行指定 JavaScript 脚本

本文总结了在Flask应用中执行JavaScript脚本的六种核心方法:1)静态JS文件引入,2)外部CDN脚本加载,3)动态数据传递(模板变量嵌入和API接口),4)函数触发(事件绑定、AJAX回调和WebSocket),5)前端框架集成,6)注意事项与方案选择建议。文中详细说明了每种技术的实现方式、适用场景及安全考量,并提供了典型代码示例。针对不同需求(静态加载、动态交互、实时响应等),给出了对应的推荐方案和技术组合建议,强调应根据项目需求选择安全且可维护的实现方式。

MrLi的博客 1009

uni-app - 解决 H5 微信公众号授权登录后,无法回退到 “上一页“ 的问题(微信登录后如何返回到触发它的那一页?无法通过 uni.navigateBack() 返回到前一页的问题?)详细教程

uniappH5微信登录重定向后无法返回到原来进入前的页面了,uni-appH5微信浏览器中授权微信登录后,无法正常回退问题,uniappH5微信登录后怎么返回上一页,uniapp公众号登陆后使用uni.navigateBack()回不去上一页,uniapp微信登录成功后返回页面后一直自动刷新页面,uniapph5授权成功后如何返回上一页,uniappH5微信登录后怎么返回到触发它的那一页去,公众号h5在授权后如何跳转回到刚进入时的页面,微信公众号H5网页返回上一页疑问,解决微信H5静默授

🏆 前端领域优质创作者 7715

如何在浏览器控制台中执行 JavaScript 模块

在Web开发过程中,有时需要在特定网站的上下文中执行脚本,比如收集网页上显示的数据、自动执行诸如点击按钮等操作,或者修改网站的某些行为。通常的做法是登录网站,打开DevTools中的控制台,然后粘贴并执行脚本。这种方法虽然直接,但存在一些不便之处。本文将介绍一种更高效的方法来在浏览器控制台中执行JavaScript模块。传统方法的局限性代码修改后需要重复从编辑器复制并粘贴到控制台。JavaScri...

粉丝们务必加入微信粉丝群 1217

Dify 第 2 篇 Dify 插件离线安装

Dify是一个支持插件扩展的AI平台,其插件市场提供5种类型插件(模型、工具、Agent策略、扩展、插件包)。由于默认pip安装速度慢,可通过dify-plugin-repackaging工具将插件转换为离线包(使用阿里云镜像加速)。该工具提供两种方式:1)自备Python环境执行脚本;2)使用Docker环境(推荐)。脚本支持从Dify市场或GitHub下载插件,并重新打包为含依赖的离线包,便于通过"本地插件"方式快速安装。

甘蓝的专栏 1267

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

浏览器、网页及JS脚本详解

浏览器如何与网页协同工作,渲染、更新与卸载的生命周期

2401_83707780的博客 1275

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

企业级浏览器安全策略实战:应对about:blank#blocked

渐进式部署:先在小范围测试,确认无业务影响后再全量推广例外管理:为确实需要特殊规则的业务保留灵活的例外机制监控机制:建立实时的策略拦截告警,快速发现问题文档同步:保持策略变更与内部文档的实时同步。

OnyxPanther23的博客 1961

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Shortkeys浏览器扩展中JavaScript执行功能的安全限制问题解析

Shortkeys是一款广受欢迎的浏览器快捷键扩展工具,其"Run JavaScript"功能允许用户通过快捷键执行自定义JavaScript代码。然而,随着现代浏览器安全策略的不断升级,该功能在某些网站上面临执行限制。 ## 问题现象 用户报告在多个主流网站如Gmail、LinkedIn和YouTube上,Shortkeys的JavaScript执行功能无法正常工作。具体表现为两种错误类型:...

gitblog_07334的博客 415

Uncaught EvalError: Refused to evaluate a string as JavaScript

【Uncaught EvalError 已解决】 本文不仅解决了问题,还介绍了问题发生的原因。。重点在于介绍了:内容安全策略,策略指令

gao_zhennan的博客 1万+

js不执行

1 标签在规范情况下可以在head内也可以在body内,但是在其他地方是不符合规范的。 特殊情况放在head内会不执行,因为JS代码先于dom执行了,所以建议 将script标签放在body内部。 2 iframe内部添加的 ...

sinat_38990768的博客 1118
上一篇: MySQL数据库备份和还原的常用命令
下一篇: JavaScript window下面的常用函数、属性
QuLong
博客等级 码龄19年 0粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值