Tag Archives: js

A method of collecting JS dynamic content by PHP

1、 Download and install phantomjs according to your own platform https://phantomjs.org/download.html

2、 Call a JS file to access the URL connection that needs to be collected

// Example using HTTP POST operation

"use strict";
var page = require('webpage').create(),
    system = require('system'),
    server = system.args[1],
    settings = {
    encoding: "utf8",
    headers: {
        "Content-Type": "text/html",
        "Cookie": "cookie",
        "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1"
    }
};

page.open(server, settings, function (status) {
    var content = page.evaluate(function() {
       return document.getElementById('uiContent').innerHTML;
    });
    console.log(content);
    phantom.exit();
});

3,

<?php
$url = $_REQUEST['url'];
$command = "phantomjs cread.js " . $url;
@exec($command,$content);
echo $content[0];

 

The difference between onready or $() and onload

1. Onready or $() is executed before onload

2. Onready or $() is executed after page parsing, while onload is executed after all elements of the page are loaded

3. Onload only executes the last one, while onready or $() can execute multiple

 

One is the different times, $(). Ready() can be used many times in an HTML, onload can only be used once, or the only valid binding.

The other is the timing of execution. In short, $() is executed earlier than onload, because $() is executed as long as the DOM tree is loaded, and onload is executed when all elements are loaded.

Encapsulation of Ajax JSON / fromdata request in native JS

ajaxPost

function ajaxPost(url,json,cb){
	var xhr = new XMLHttpRequest();
	xhr.open('post',url);
	xhr.setRequestHeader("content-type","application/json");
	xhr.send(JSON.stringify(json));
	xhr.onload = function(){
		cb(JSON.parse(xhr.responseText));
	}
}

Posting pictures

function ajaxPic(formObj,url,cb){
	var formData = new FormData(formObj);
	console.log(formData);
	var xhr = new XMLHttpRequest();
	xhr.open('post',url);
	xhr.send(formData);
	xhr.onload = function(formData){
		cb(JSON.parse(xhr.responseText));
	}
}

JS error: unexpected token u in JSON at position 0

At first glance, this error is confusing, but it is simply due to json.parse parsing of undefined.

JSON.parse is used somewhere, but the argument passed in is not a qualified JSON string.
Parse parse parse parse parse parse parse parse parse parse
Parse before you judge
 


Come from:
https://www.cnblogs.com/yangxunwu1992/p/8964780.html

Setter and getter of JS class attribute

You can use the GET and SET keywords inside the “class” to set the save and accessor functions for a property and intercept the access behavior of that property.

class A {
  constructor () {
    this._a = 'a'
  }
  set a (val) {
    if (Object.prototype.toString.call(val)  !== '[object String]') {
      this._a = 'error'
    } else {
      this._a = val
    }
  }
  get a () {
    return this._a
  }
}

let obj = new A()
console.log(obj.a) // a
obj.a = 123
console.log(obj.a) // error

about jQuery.js The solution to always reporting errors

1, this method is seen on a post, thought that great god provides methods, his blog: http://blog.csdn.net/a1769075789, but the great god didn’t write anything. sorry
JS package always report an error:
The window – & gt; Preference – & gt; MyEclipse-> Click on the validation

Find JavaScript Validator for JS Files in the table on the right and remove the two checks

Save, close.

2. Mutantan – The great god’s blog also has a solution, but has not tried it

Cross origin requests are only supported for

In this paper, the scene
When writing HTML using VS Code, the imported JS file needs to import modules across domains.
why
Use File protocol, need to use HTTP, Data, Chrome-Extension, Edge, HTTPS and other protocols, commonly speaking is the need to use the server to open a port.
The solution
Right click directly in VS Code and select Open with Live Server.

Transformation of JS map and JSON

I was going to store the map as a JSON string in window.localStorage, but I found that I could not convert the map to a JSON string

var map = new Map(); 
map.set(21,'A');
map.set(22,'B');
console.log(JSON.stringify(map));


ah

Traversing the background data to generate tree structure

var getTree=function(treeData,parentId){
var treeArr=[];
for(var i=0; i< treeData.length; i++){
var node=treeData[i];
if(node.sjchannelcode==parentId ){
var newNode={order:node.order,code:node.channelcode,url:node.url,name:node.name,sjchannelcode:node.sjchannelcode,channelcode:getTree(treeData,node.channelcode)};
treeArr.push(newNode);
}
}
return treeArr;
}
// call tree method
var treeArr=getTree(data,sj);
data is the data returned from the background, sj root directory returned by the node parent id

Install and run phantomjs on MAC

Maybe it’s too easy?There are no tramp tutorials on Search.com.

Search phantomjs on your MAC and get the following method:

Download Phantomjs.zip and unzip. Bin/Phantomjs is directly available.

Or it can be installed through Homebrew

brew update & & brew install phantomjs

Or install via MacPorts

sudo port selfupdate & & sudo port install phantomjs

These are the three ways to do it, and they all make a dent.

Let’s start with the method that will ultimately make my installation a success: using Homebrew.

What is Homebrew?

The Missing Package Manager for OS X (The indispensable Package Manager for OS X)

Installation:

It is very simple to execute the command line directly from the MAC terminal

The/usr/bin/ruby – e “$(curl – fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

Testing:

Enter BREW – V to see the version number, indicating a successful installation.

Install phantomjs:

Type BREW Update (I didn’t enter it, the next install will automatically help me update.)

Wait until the update is complete before typing
Brew Install Phantomjs. Installed successfully! Enter the PhantomJs-V view number to confirm.

A simple hello. Js:

console.log(“Hello,world!” );

phantom.exit();

Run:

The directory where the JS file is running on the CD

Phantomjs hello. Js

Output Hello, world!

When nodejs is installed: the error code is 2503

When installing Nodejs under Windows, you always report an error:

The installer has encountered an unexpected error installing this package. This may indicate a problem with this package. The error code is 2503.

There are three ways to try it:

    “Run as administrator” obtains administrator authority to Run the installation file. In Linux, you can prefix the command line with sudo. Open the “Start” menu in the lower left corner of the computer and type CMD in the search bar of the popup menu bar.
    in the top list, right-click [CMD] and select [run as administrator];
    enter the command line interface, find the directory of the program to be installed, and copy the path in the address bar;
    , for example, want to install the node-v6.9.1-x64.msi file under D disk;
    type D:\node-v6.9.1-x64.msi on the CMD command line;
    (be sure to have the full address, including the name of the installer); Right-click on the package and select the properties option.
    enter the properties dialog box, click the “security” option at the top, then select the current computer user, and click the lower right corner;
    enter the edit bar, check [full control] at the permissions below, and then click ok to install.

Author: leedaning
this address: http://blog.csdn.net/leedaning/article/details/53138664

Keep alive in Vue

keep-alive
Keep-alive is an abstract component built into Vue that allows contained components to remain in state, avoiding repeated component creation and rendering
Keep-alive is generally used in combination with routing and dynamic components to cache components.

<keep-alive>
    <component :is='current'></component>
</keep-alive>

Keep-alive provides include and exclude attributes, both of which support strings or regular expressions. Include means that only components with matching names will be cached. Exclude means that any components with matching names will not be cached.

<keep-alive :include="includeList" :exclude="excludeList">
    <router-view></router-view>
</keep-alive>

Keep-alive caches include matching components and does not cache exclude matching components.