Call href from JavaScript - javascript

This is the same question as THIS ONE, I can't answer that anymore, so I'm re-posting it with my account.
Sorry for the mess.
I need a Greasemonkey script that on a page load activates a href link like 'javascript:FUNCTION'.
I've seen this code:
<script language="Javascript" type="text/javascript">
function somescript() {
window.location.href = document.getElementById('ololo').href;
}
</script>
test
<br />
click me
and, while it works on a local page even when using onload, it doesn't work when I use it in my script.
Probably I'm missing something when transferring the code from the body of an html page to a Greasemonkey script.
I hope this time the question is more clear, excuse me for any misunderstanding, but I'm still a beginner with JS.

Solved it like this:
window.location=document.getElementById('foo').href;
Thanks everyone for answering anyway.

<script type="text/javascript">
function somescript() {
eval(document.getElementById('ololo').getAttribute('href').replace('javascript:', ''));
}
</script>
I can see the alert box..
Please note that this will only work when its javascript code into the href attribute...

Will this work for your scenario?
<script type="text/javascript">
function somescript() {
document.getElementById('ololo').click();//fake a click on the link
}
</script>

Related

Owl carousel does not work unless I refresh the page

Hello my problem with this library that does not load me on the first page, instead if I "refresh" if it loads, and the internet explorer does not work for me. someone has happened something similar. I don't know what I'm doing wrong, I put a link and a capture of the error. THANK YOU https://ccc.es/empresa/ https://ibb.co/3fXLPyH
<script type='text/javascript' src='https://ccc.es/wp-content/themes/norebro/assets/js/libs/owl.carousel.min.js?ver=5.4.2'></script>
I have found a possible solution from another colleague who passed by here I copy it, I don't know if this solution can be improved, but it works for me for now
`<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.3.4/owl.carousel.min.js"></script>
<!--DO NOT ENTER ANY EXTERNAL LINK IN BETWEEN-->
<script type="text/javascript">
$(document).ready(function() {
$('.owl-carousel').owlCarousel({
loop: true,
});
});
</script>`

Simple code only works in certain editor but not anywhere else

Why does the below code only work in the Codecademy.com editor and not in a real browser or jsfiddle? There seem to be no mistakes...
HTML
<p id='p1'>Zebras eat bananas</p>
<p id='p2'>That's nonsense</p>
With this javascript, #p2 should change automatically when the page has loaded. In the Codecademy editor that's what happens, but see it not work in Jsfiddle and also not when I make HTML/js files and run it in my browser.
How can I fix this? Or did they teach me wrong?
Javascript
$(document).ready(function(){
$('#p2').html("Now it makes sense!");
})
As #dfsq has mentionend, you need to include jQuery to your file, as to your fiddle. You can do this by adding this line before your script:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
After that, you can put your code in <script> tags:
<script>
$(document).ready(function(){
$('#p2').html("Now it makes sense!");
})
</script>
and you will see that it works.
Your jQuery script should allways be directly before the closing </body> tag, so that the DOM is loaded before you try to access it with javascript/jQuery

Is there a way to use javascript code without using <script> tag?

So I'm working on my online portfolio using prosite.com and I created some simple hover thingy using javascript (http://wojtek.szukszto.com/index.html). The problem is prosite.com won't allow me to use < script > tag... Is there any way to do it? Maybe as an external html? I don't know... I'm not really good in coding, so any help would be appreciated.
You can have them as DOM Events like
<div onclick="alert('cat');">
I <strong>Really</strong> want a cat!
</div>
<body onload="//you can put a whole bunch of stuff here"></body>
(It is equivalent to window.onload = function(){ //stuff })
You can put your javascript code into external file and call in the head section of your html document
<head>
<script src ="/JS/yourjsfile.js"></script>
</head>
hope that your host allows you to call JS in the head section
The way I do it is by stating the type of script. The ProSite already has javascript integrated within itself. I use:
<script type="javascript"> Code-Goes-Here </script>

DatePicker Does Nothing When Clicked

I'm trying the simplest example of getting datepicker to work, and I just can't seem to get it. There is almost nothing in my fiddle.
The top of my web page has this:
<script src="~/Scripts/jquery-1.6.2.js" type="text/javascript" />
<script src="~/Scripts/jquery-ui-1.8.11.js" type="text/javascript" />
<script>
$(function(){
$("#datepicker").datepicker();
});​​​
</script>
Note: I tried putting the first two script lines in _Layout.cshtml, but I got an error (don't recall the error at the moment). That's why I just put it all on the one page.
And down a bit, in that same page, is this:
<td><input type="text" id="datepicker"></td>
When I click on the input control, nothings happens. However, when I click on the control in the fiddle, it works. What am I missing?
Also, you'll notice I'm using jquery 1.6.2 and jquery-ui 1.8.11. That's different than the fiddle example because fiddle didn't provide those versions as options. I'd be surprised if the version was the difference.
Script tag must have both opening and closing items. You cannot shorthand close them.
Instead of this
<script src="~/Scripts/jquery-1.6.2.js" type="text/javascript" />
do this
<script src="~/Scripts/jquery-1.6.2.js" type="text/javascript"></script>
I'm hoping this won't be the final answer, but I finally got the datepicker to display. After much trial and error, and research, I seem to have a jquery conflict, but I don't know why. Someone suggested something called non-conflict mode. So I changed my datepicker code to this:
<script type="text/javascript">
(function ($) {
$(document).ready(function () {
$("#endTime").datepicker();
});
})(jQuery);
</script>
And it worked. I'm not sure why, but it's finally at least working. If anyone can provide more insight, or a better answer, I'd gladly pick it.
The above example is explained in this answer: Basically, what I did was define a function that takes a parameter (the $) and then execute that function with jQuery as the parameter.
What I still don't understand though, is why this code won't work:
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#endTime").datepicker();
});
</script>
If there was a conflict because of the $, and I replaced $ with jQuery, shouldn't it work?
The datepicker is pretty much straight forward why dont you use something like firebug in mozilla to see if it is throwing some error also date picker internally uses the Jquery theme roller so please download and add the jquery "css/custom-Theme/jquery-custom.css" css.

Call an anchor tag via javascript

I am using thick box 3.1 to load a pop up. It working well by using in the following way:
TEST
If we click on the TEST now then the popup is working well and good.
Now my prob is: I need to call this popup in form load using JavaScript.
I do something like below:
<script type="text/javascript">
window.location.href = "filename.php"
</script>
it's just redirecting to that particular file. But not showing in the pop up.
What is the possible way?
thanks in advance
Try this:
Test
<script type="text/javascript">
$(function(){ // On DOM ready
$('#openOnLoad').click();
});
</script>
You can do this without changing your markup, like this:
$(function() {
$('a[href=filename.php]').click();
});
TEST
<script type="text/javascript">
$("#UniqueIdForThisLink").click();
</script>

Categories