I have this javascript code that inserts a label into an existing html page. However, it only shows when i reload the page. Like if i visit the page it doesn't show. once im at the page and reload, it then shows. How do i fix it so it shows right when i visit the page?
$(document).ready(function () {
insertlabel();
});
function insertlabel() {
var $label = $('<label>test</label>');
//place them on the page
$('.div1').after($label);
}
I'm not sure if it makes a difference, but I always define my functions inside $(document).ready();
$(document).ready(function () {
function insertlabel() {
var $label = '<label>test</label>';
$('.div1').after($label);
}
insertlabel();
});
Related
I'm trying to automatically click a href link when the page loads.
<div class="loadmore" kind="rb">
تحميل المزيد...
</div>
The link is supposed to load more comments when you click it.
I tried this:
window.onload = function() {
autoloadmore()
};
function autoloadmore() {
var loadmoreClass = document.getElementsByClassName("loadmore")[0];
var loadmoreChild = loadmoreClass.getElementsByTagName("a");
if(loadmoreClass){
loadmoreChild[0].click();
}
}
but it doesn't seem to work. The problem seems to be with click(), because when I use other codes such as .style.color, they work.
I'm using Blogger by the way.
This's a sample blog:
http://blog-sample-001.blogspot.com/2018/09/title.html
(go to تحميل المزيد...)
ps: I can't change the div.
Assign some ID to تحميل المزيد... . For example, clickAfterPageLoad or whatever you want, and then try this...
$(document).ready(function() {
$("#clickAfterPageLoad").trigger('click');
});
or if you cant't change div, you can do like this:
$(document).ready(function() {
$(".loadmore a").trigger('click');
});
I have problem with my code, because I try to do a visible appended div after refresh page or if someone go for another page.
In file .txt is code to run customer chat Facebook on my page.
My code looks like that:
jQuery(document).ready(function ($) {
$('.w-iconbox.iconpos_left.style_default.color_primary.no_text.ico-fbm').one('click', function (e) {
$('<div class="facebook-chat"></div>').load("/fbchat.txt").appendTo("body")
localStorage.setItem("fb-opened", $('.facebook-chat').html());
setTimeout(function () {
$('fb_dialog_mobile').click();
}, 4000);
})
$('.facebook-chat').html(localStorage.getItem("fb-opened"));
})
Problem is that when I go for another page or refresh page, the chat div disappears -
how could I fix it to keep it in localstorage e.g. throught 5 hours?
Just syntax. You're using one and not on.
jQuery(document).ready(function ($) {
$('.w-iconbox.iconpos_left.style_default.color_primary.no_text.ico-fbm').one('click', function (e) {
$('<div class="facebook-chat"></div>').load("/fbchat.txt").appendTo("body")
localStorage.setItem("fb-opened", $('.facebook-chat').html());
setTimeout(function () {
$('fb_dialog_mobile').click();
}, 4000);
})
$('.facebook-chat').html(localStorage.getItem("fb-opened"));
})
I changed the menu on my website so it replaces the main content on my page with html files using jquery instead of just href-ing the html pages. Something like:
$(function() {
$('.new_page_title').on('click', function (e) {
e.preventDefault();
$('.main_content').load('new_page.html');
$('body').scrollTop(0);
});
});
For one of my pages I want to have a rotating sentence that gets randomly chosen from an array. Before, I was just doing it on page load, but now I have added a line to the above function to call the randomizer which looks like:
function make_a_sentence() {
var sentenceArray = [
'digging holes in the back yard',
'eating our vegetables',
'doing something important',
'building the future',
'watering my plants',
'propogating succulents',
'watching Call the Midwife',
'looking outside and dreaming of salvation',
'poopin\'',
'providing for my family',
'watching you through your window',
'digging around craigslist',
'rotating my tires',
'settling lawsuits'
];
var selected = Math.floor(sentenceArray.length * Math.random());
$("#id_of_div").html(sentenceArray[selected]);
}
And then calling make_a_sentence() at the end of the click function. When I just load the page, the sentence doesn't show up, and when I debug it inserts the sentence into the div until it gets to the end of the debug and then the sentence disappears. Any ideas?
The load function is asynchronous, you need to run code dependent on the loaded page's content in load's callback (otherwise your changes gets rewritten as they occur before the load is completed), like:
$(function() {
$('.new_page_title').on('click', function (e) {
e.preventDefault();
$('.main_content').load('new_page.html', function () {
$('body').scrollTop(0);
make_a_sentence()
});
});
});
See jQuery.load for reference.
The following code works perfectly fine for "normal" wordpress pages without a parent page, BUT if the requested page has a parent I get nothing and I can't get the code to work (alert box never came) after 5h of web-searching and trial and error.
JS/jQuery here:
<script>
jQuery(document).ready(function () {
//get the page via page slug
jQuery.get("thePageSlug").success(function(data){
//get the content div of the found page
jQuery(data).find("#content").appendTo("#content");
alert("finally.....");
});
});
</script>
I think this is because you used relative path in .get function. Try to use absolute path so that it will not get conflict with Wrodpress Permalink.
Try this instead:
jQuery.get("/thePageSlug")...........
Or even:
jQuery.get("http://your.domain/path/to/your/thePageSlug")...........
Code:
<script>
jQuery(document).ready(function () {
//get the page via page slug
jQuery.get("<?php echo site_url(); ?>/thePageSlug").success(function(data){
//get the content div of the found page
jQuery(data).find("#content").appendTo("#content");
alert("finally.....");
});
});
</script>
I am newbie to the Asp.net so please bear with me for this :)
I added a hyperlink html tag like this
<h2><a class="iframe2 cboxElement" id="frameEdit" onclick="return OpenEditPage()">(EDIT)</a></h2>
Here is the OpenEditPage() method
<script>
function OpenEditPage()
{
var pid = document.getElementById("ProjId").value;
var link = document.getElementById("frameEdit");
link.setAttribute('href', "/Project_MainPageEdit.aspx?editpid=" + pid);
return false;
}
</script>
As soon as I click EDIT hyperlink, edit page is opened in Iframe. But when i close the edit page, I return back to MainPage but all the buttons and other elements on the page become unresponsive until I refresh the page.
What's the reason behind this?
I added following code inside the script and it worked like a charm
$(document).ready(function ()
{
$(".iframe2").colorbox({ onClosed: function () { window.location.reload(); } });
});