<a href="/feed" data-virtual>Feed</a>
<a href="/news" data-virtual>News</a>
<a href="/users" data-virtual>Users</a>
<a href="/users">Real link</a>
document.body.addEventListener("click",function(evt){
var target=evt.target;
if(target.tagName=="A"){
if(target.hasAttribute("data-virtual")){
window.history.pushState({},target.textContent,target.href);
evt.preventDefault();
alert("Идем на "+target.href);
}
}
})
window.addEventListener("popstate",function(evt){
alert("Назад на "+window.location.href);
})
.block{
animation: upper 1s linear infinite;
}
var testFunc=new Promise(function(resolve,reject){
setTimeout(function() {
//down();
resolve();
}, 10000);
});
testFunc.then(function(){
console.log('Result function: true');
},function(err){
console.log('Result function: false');
});
function testFunc(){
return new Promise(function(resolve,reject){
setTimeout(function() {
//down();
resolve();
}, 10000);
});
}
testFunc().then(function(){
console.log('Result function: true');
},function(err){
console.log('Result function: false');
});
var obj = {
MainService: [
{
Id: "733",
Name: "service",
MainService: [
{
Id: "238",
Name: "service 1",
Service: [
{
Id: "145",
Name: "Service 2"
}
]
}
]
}
]
};
function mapObject(obj,asArrays){
var paths=[];
function scan(obj,stack){
var k, v, path;
for(k in obj){
if(obj.hasOwnProperty(k)){
path=stack.concat([k]);
if((v=obj[k])!==null&&typeof v=='object'){
scan(v,path);
}else{
paths.push(asArrays?path:path.join('.'));
}
}
}
}
scan(obj,[]);
return paths;
}
console.warn(mapObject(obj));
console.warn(mapObject(obj,true)); //вдруг где пригодиться ;)
<a href="#x=123&y=100">Scroll to x/y</a>
<a href="#scrollTo=#scrollID">ScrollTo element selector (id)</a>
<a href="#scrollTo=.class">ScrollTo element selector (class)</a>
<a href="path/to/page.php#scrollTo=.class">ScrollTo element selector (class)</a>
function decodeScrollPos(hide) {
var params = {}, el, loc = window.location, hash = loc.hash.substr(1);
hide && (loc.hash = "");
hash.split('&').forEach(function (raw) {
var data = raw.split('=');
data[0] && (params[data[0]] = data[1] || "");
})
setTimeout(function () {
if (params.x || params.y) {
console.log('scroll=', hash);
window.scrollTo(params.x, params.y);
} else if (params.scrollTo) {
(el = document.querySelector(params.scrollTo)) && el.scrollIntoView && el.scrollIntoView(true);
}
}, 100);
return params;
}
window.onhashchange = function () {
decodeScrollPos(true);
}
console.log(decodeScrollPos(true));
window.location.hash="";
<div id="fields">Контейнер полей</div>
<a onclick="fields.add('input');">Add</a>
<a onclick="fields.remove();">Remove</a>
var FieldSet=function(container,tagID){
this.fields=[];
this.container=(typeof container=='object')?container:document.getElementById(container);
var id=0;
this.add=function(type){
var template={input:'TextField Name : <input type = "text" name="txt_field"> -> your text field has been generated just define name',
textarea:'',
table_name:''}[type],
wrapper=document.createElement("div");
wrapper.innerHTML=template;
tagID&&wrapper.setAttribute("id",tagID+(id++));//можно выбросить если не надо id для полей
this.container.appendChild(wrapper);
this.fields.push(wrapper);
return wrapper;
}
this.remove=function(index){
this.container.removeChild(this.fields.pop());
}
}
var fields=new FieldSet("fields");//id или элемент контейнера для коллекции полей
function wrapText(str, index, count, color){
return [
str.substr(0,index),
'<span style="color:',
color,
'">',
str.substr(index,count),
'</span>',
str.substr(index+count)
].join("");
}
document.querySelector('div').addEventListener('input', function() {
var s=this.innerText;
this.innerHTML=wrapText(s, s.length-1,1,"red");
}