I have two script in javascript/jquery. How i will combine in one. On body load this function run
<script type="text/javascript">
window.onload=function() {
var tId = setInterval(function() {
document.getElementById("link1").disabled=!navigator.onLine;
},250);
</script>
<script>
var status = navigator.onLine;
if (status) {
$("#link1").removeClass('diconnected').addClass('connected');
$("#link1").data('disabled',true);
} else {
$("#link1").removeClass('connected').addClass('diconnected');
}
}
</script>
$(document).ready(function(){
var status = navigator.onLine;
if (status) {
$("#link1").removeClass('diconnected').addClass('connected');
$("#link1").data('disabled',true);
} else {
$("#link1").removeClass('connected').addClass('diconnected');
}window.onload=function() {
var tId = setInterval(function() {
document.getElementById("link1").disabled=!navigator.onLine;
},250);
}});
try this
Your first script is missing a closing bracket }
If you add in a } you can remove the closing and opening script tags.
Additionally, you may want the second bit of code within the window.onload function, which would mean you could put the closing bracket } after the 2nd scripts code
Related
I would like to implement the second code into the first one in order for it to work as intended. I am unsure how to do this
Current code:
<script type="text/javascript">
!function(){
var campaign_link = ""; // REPLACE WITH YOUR LINK
var t;
try{
for(t=0;10>t;++t)history.pushState({},"","#");
onpopstate=function(t){t.state&&location.replace(campaign_link + window.location.search.substring(1))}}
catch(o){}
}();
</script>
the code I would like to add to existing code;
window.addEventListener("popstate", function(e) {
window.location.href = location.href;
});
You can just add this function to your current script like so. However you may want to store the loction.href in a var as opposed to window.location
<script type="text/javascript">
function(){
var campaign_link = ""; // REPLACE WITH YOUR LINK
var t;
try{
for(t=0;10>t;++t)history.pushState({},"","#");
onpopstate=function(t){t.state&&location.replace(campaign_link + window.location.search.substring(1))}}
catch(o){}
}();
window.addEventListener("popstate", function(e) {
window.location.href = location.href;
});
</script>
I have some variables in the following JS:
document.addEventListener('DOMContentLoaded', function (){
document.getElementById('btn4').addEventListener('click', getbg);
});
getbg = function()
{
chrome.runtime.getBackgroundPage(
function (bg) {
var allcompanynames = bg.companynames;
alert(allcompanynames)})
}
As you can see, the variable is "allcompanynames".
However, how do I pass them and show it on the popup.html page?
I have tried
<script type="text/javascript" src="companynames.js"></script>
<p id="allcompanynames"></p>
no luck. What's wrong?
document.addEventListener('DOMContentLoaded', function (){
document.getElementById('btn4').addEventListener('click', getbg);
});
getbg = function()
{
chrome.runtime.getBackgroundPage(
function (bg) {
var allcompanynames = bg.companynames;
alert(allcompanynames)})
document.getElementById("allcompanynames").innerHTML(allcompanynames)
}
I'm guessing you should add that last line after displaying the pop up to add the content into the page.
Write your code in this way
var background = chrome.extension.getBackgroundPage();
var allcompanynames = background.companynames;
alert(allcompanynames)
The javascript on the page needs to work onpage load. So I tried adding the document ready function into the code. It doesn't seem to work.
http://janeucreative.com/daddychallenge/bag.html
<script>$(document).ready(function() {
function addItem(item) {
var itemInCart = item.cloneNode(true);
itemInCart.onclick = function() { removeItem(this); };
var cart = document.getElementById("cart");
cart.appendChild(itemInCart);
}
function removeItem(item) {
var itemInItems = item.cloneNode(true);
itemInItems.onclick = function() { addItem(this); };
var cart = document.getElementById("cart");
cart.removeChild(item);
}
init();
});</script>
Any advice would be much appreciated! I'm very new to javascript and just trying to learn it a step at a time.
First, you'll want to have your page using a more modern version of jquery:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.js" ></script>
Then, AFTER linking the jquery, which you have done correctly, you can rewrite your code like this:
<script>
function init(toAdd, toRemove){
addItem(toAdd);
removeItem(toRemove);
}
function addItem(item) {
var itemInCart = item.cloneNode(true);
itemInCart.on("click",function(){removeItem(this);});
$("#cart").appendChild(itemInCart);
}
function removeItem(item) {
var itemInItems = item.cloneNode(true);
itemInItems.on("click", function(){addItem(this);});
$("#cart").removeChild(item);
}
init($("#myAddedItem"), $("myRemovedItem"));
</script>
This way, you'll have your functions addItem and removeItem available elsewhere on the page. You currently seem to have them set up to run only once, from the page initialization.
I'm not sure what you're doing with cloneNode.
It´s posible , if i have one var , use jquery for load news vars and change these
For example if i have :
var activate_auto="yes";
$("#play").click(function() {
/// load vars
activate_auto="yes";
});
$("#stop").click(function() {
/// load vars
activate_auto="no";
});
COMPLETE CODE
<script>
var w_ac="900";
var h_ac="295";
var w_ac_col="64";
var n_cols="5";
var margin="3";
var time_ac_change=5000;
var time_ac_effects=2000;
var time_ac_hide_effects=1000;
var ac_activate_auto="si";
var ac_activate_auto_random="no";
var margin=margin*n_cols;
var result=w_ac_col*n_cols+margin;
var final=w_ac-result;
$(function(){
$("#ac_play").click(function()
{
ac_activate_auto="si";
ac_activate_auto_random="no";
time_ac_change=2000;
});
$("#ac_stop").click(function()
{
ac_activate_auto="no";
ac_activate_auto_random="no";
time_ac_change=2000000;
});
for (i=1;i<=n_cols;i++)
{
$(".ac_"+i+"").css("width",""+w_ac_col);
}
});
function acordeon(id)
{
$(function(){
for (i=1;i<=n_cols;i++)
{
if (+id==i)
{}
else
{
$(".ac_"+i+"_inf").hide(time_ac_hide_effects);
}
}
$(".ac_"+id+"_inf").css("width",""+final);
$(".ac_"+id+"_inf").show(time_ac_effects).fadeIn(time_ac_effects);
$(".ac_im").css("width",""+final);
$(".ac_im").css("height",""+h_ac);
});
}
</script>
<script>
var starting_slide = n_cols;
function setSlide(i)
{
acordeon(i);
if (i > 0) {
i--;
setTimeout( function(){setSlide(i)}, time_ac_change);
}
else
{
setTimeout( function(){setSlide(starting_slide)}, 1000);
}
}
if (ac_activate_auto=="si")
{
setSlide(starting_slide);
}
if (ac_activate_auto_random=="si")
{
$(document).ready(function(){
acordeon(1);
var refreshId = setInterval(function(){
var r=Math.floor(Math.random()*n_cols)
if (r==0)
{
acordeon(1)
}
else
{
acordeon(+r)
}
}, time_ac_change);
});
}
</script>
In this case works always when i push over pay or stop but only one time , if i push over play works play but no works before stop and the same when the first i push it´s play
I want push stop load new var and play and other time load the other var , i don´t know if it´s possible , thank´s !
You should only create your var and don't set a value to it, and then, only set its values inside your functions. Ex.:
var activate_auto; // this is outside your click functions
Ok, now I'm not sure what you want to do... will edit it later if necessary =\
Need to apply a var to a statement if its conditions are met, this syntax isn't throwing errors but its not working.
<script type="text/javascript">
$(document).ready(function(){
var action_is_post = false;
//stuff here
$(this).ready(function () {
if ($("#stepDesc0").is(".current")) {
action_is_post = true;
}
});
//stuff here
</script>
should I use something other than .ready? Do I even need the $(this).ready(function ()... part? I need it to apply the var when #stepDesc0 has the class current.
$(document).ready(function(){
var action_is_post = false;
$("form").submit(function () {
action_is_post = true;
});
if ($("#stepDesc0").is(".current")) {
var action_is_post = true;
}
window.onbeforeunload = confirmExit;
function confirmExit()
{
if (!action_is_post)
return 'Using the browsers back, refresh or close button will cause you to lose all form data. Please use the Next and Back buttons on the form.';
}
});
<script type="text/javascript">
$(document).ready(function(){
var action_is_post=$("#stepDesc0").is(".current");
});
</script>
If you want the variable to be accessible outside the $(document).ready(function(){..., then you'll need to declare it outside the statement like this:
<script type="text/javascript">
var action_is_post;
$(document).ready(function(){
action_is_post=$("#stepDesc0").is(".current");
});
</script>
HTML (in order to test it):
Show value
$(function() {
var actionIsPost = $('#stepDesc0').is('.current');
alert( actionIsPost );
});
If you are adding the current class to #stepDesc0 on an event then put the .is check in the event handler.