I have a script in the footer of a site. I want the widget to be shown if url contains 'mystring', and if not, then not to be shown
<script>
if (window.location.href.indexOf("mystring") > -1) {
<script type="text/javascript" src="https://vk.com/js/api/openapi.js?169"></script>
<!-- VK Widget -->
<div id="vk_community_messages"></div>
<script type="text/javascript">
if($(window).width()>800) {
VK.Widgets.CommunityMessages("vk_community_messages", 203651941, {expandTimeout: "20000",tooltipButtonText: "Any Question?"});
}
if($(window).width()<=800) {
VK.Widgets.CommunityMessages("vk_community_messages", 203651941, {tooltipButtonText: "Any Question?"});
}}
</script>
This code produces the following errors:
Uncaught SyntaxError: Unexpected identifier (string 3)
Uncaught SyntaxError: Unexpected token '}' (string 12)
You've got <script> tags and other HTML inside the outer <script> tags, that simply isn't allowed and isn't ever going to work. This is a fairly basic thing you should have learned when first learning about HTML and JavaScript.
This will make more sense (I can't test it easily so no guarantees it will work exactly, but it certainly won't have any fundamental design flaws or syntax errors, so it should point you down the right road):
<div id="vk_community_messages"></div>
<script type="text/javascript" src="https://vk.com/js/api/openapi.js?169"></script>
<script>
if (window.location.href.indexOf("mystring") > -1) {
if($(window).width()>800) {
VK.Widgets.CommunityMessages("vk_community_messages", 203651941, {expandTimeout: "20000",tooltipButtonText: "Any Question?"});
}
if($(window).width()<=800) {
VK.Widgets.CommunityMessages("vk_community_messages", 203651941, {tooltipButtonText: "Any Question?"});
}
}
</script>
Related
I am having issues trying to run an API using Geocodio. This is set up through WordPress and I would really appreciate any help I can get!
Everything works up until the actual API itself has to run. The Geocodio documentation states that it runs "using a jQuery AJAX call". I have tried multiple script sources that claim to fix the problem, but I get the error that "superfish() is not a function" or that "Cannot read properties of undefined (reading 'get')".
<head>
<script src="jQuery.js"></script>
<script src="superfish.js"></script>
<script>
jQuery(document).ready(function() {
jQuery('ul.sf-menu').superfish();
});
</script>
</head>
<body>
<input type="text" id="myTxt" style="width: 400px;" placeholder="Type Your Address Here!"></input>
<input type="submit" id="myBtn" value="Search Now!"></input>
<script>
var submit = document.getElementById("myBtn")
var myInput = document.getElementById("myTxt")
submit.addEventListener("click", geocodio)
function geocodio() {
if(myInput.value.length == 0) {
alert("Please input a valid address")
}
else {
$.get('https://api.geocod.io/v1.7/geocode?q='+ encodeURIComponent(myInput)
+'&api_key=' + encodeURIComponent('YOUR_API_KEY'), function (response) {
console.log(response.results)});
}
};
</script>
</body>
You must be using some strange version of jQuery that forces you to refer to it as jQuery and not $. Odd, I thought all versions of jQuery also set up the $ global.
Try replacing $ with jQuery inside geocodio():
function geocodio() {
if (myInput.value.length == 0) {
alert("Please input a valid address")
}
else {
jQuery.get('https://api.geocod.io/v1.7/geocode?q=' + encodeURIComponent(myInput.value)
+ '&api_key=' + encodeURIComponent('YOUR_API_KEY'), function (response) {
console.log(response.results);
});
}
}
(also fixed: encode myInput.value instead of encoding whole input)
If that doesn't work, perhaps jQuery.js failed to load. Check the console messages and/or network trace tab in the developer tools.
If that doesn't work, try stepping thru the code using the JavaScript debugger built in to your browser (short tutorial video).
I'm getting the following error in my notify.js :
Uncaught ReferenceError: Notify is not defined
here is the code in my js
function onPermissionGranted() {
doNotification("Notification Allowed", "You will now receive notifications once your checking is complete.")
}
function onPermissionDenied() {
console.warn("Permission has been denied by the user")
}
function doNotification(t, e) {
new Notify(t,{
body: e
}).show()
}
window.onbeforeunload = function() {
if ("" != $("#live").val() || "" != $("#cclist").val())
return "You're about to leave this page. Are you done saving your live results?"
}
,
Notify.needsPermission && Notify.requestPermission(onPermissionGranted, onPermissionDenied);
var timer, list, tlist, tw = 191, stop = !0, ct = 0, akey = "10001|BB21C00153BEEE1A0419ED806D47E63E9054CA1CAA0DEF2DBC4FEB9365992243F33707C0EEC6B3643EB77D41D6BF48F2F015C83CCAE6CC7D1C54D3ECEAC45E00A443AFFD8E2642EAB3A68BCE0841E7FB3EC257D51BBA3A35FD378D551E5F08BDB55F9CDBCF7F363E1857D85B0B62760294FFC8395ECEC42C9BC3EDC71E06FA7D188C594F1D17D4392308F1123CAA89DF1099EF729DA723654FBEFCD9884885DD3EEED42E97C462788D685587518FB62883AF0739594D67C42F77466A276D34AD1461FA60DCB0A5A2AFC9A02F4E800CA9DB2AC5C010697CF0F0479E9710F849A052E949DC9082E7A67E43A9BF722354AB3616F1C4ECACD616950CCED7702C5A17";
when I run the page I got the console error.
Your Notify class is most likely in a different file that is not referred in the file where you get this error.
The error
Uncaught ReferenceError: [ref] is not defined
is an error telling you your code has a reference that cannot be understood by the JavaScript interpreter and that it can't... well... interpret it. [ref] is the name of the reference triggering the error. It can be an unknown class, an undefined variable or an undefined function. Check for typos and make sure the class is both declared and included.
Also, make sure that the imported JavaScript file of your class is imported before the script using it :
<script src="path/notify.js" type="text/javascript" />
<script src="path/usingnotify.js" type="text/javascript" />
would work, whereas
<script src="path/usingnotify.js" type="text/javascript" />
<script src="path/notify.js" type="text/javascript" />
would not.
I get this error on this page:
http://pages.purevisionmethod.com/myopia-class-replay/
I try to get this code execute:
<div style="width:1240px;max-width:100%;text-align:center;margin:5rem auto 0 auto;">
<script src="//fast.wistia.com/assets/external/E-v1.js" async></script><div class="wistia_responsive_padding" style="padding:56.25% 0 0 0;position:relative;"><div class="wistia_responsive_wrapper" style="height:100%;left:0;position:absolute;top:0;width:100%;"><div class="wistia_embed wistia_async_4uf1zbu4se videoFoam=true" style="height:100%;width:100%"> </div></div></div></div>
<script>
window._wq = window._wq || [];
wistiaEmbed.bind("secondchange", function (s) {
if(s === 20) {
$('.hiding').fadeIn(500);
}
});
</script>
I get the error: “Uncaught ReferenceError: wistiaEmbed is not defined”
I cannot work out what's going on. The reference should be defined by
<script src="//fast.wistia.com/assets/external/E-v1.js" async></script>
But no matter where I move the file, if in the header, footer, anywhere, I still get the same error.
Can you see why?
Thanks!!
Sorry, I used the wrong type of code to integrate this.
So, for anyone using Wistia who bumps into the same problem:
You need to use the API code to get this to work:
<div class="wistia_responsive_padding" style="padding:56.25% 0 0 0;position:relative;"><div class="wistia_responsive_wrapper" style="height:100%;left:0;position:absolute;top:0;width:100%;">
<div id="wistia_INSERT_YOUR_VIDEO_ID_HERE" class="wistia_embed" style="height:100%;width:100%"> </div></div></div>
<script src="//fast.wistia.com/assets/external/E-v1.js" charset="ISO-8859-1"></script>
<script>
wistiaEmbed = Wistia.embed("INSERT_YOUR_VIDEO_ID_HERE");
</script>
<script>
(function($){
wistiaEmbed.bind("secondchange", function (s) {
if(s === 20) {
$('.hiding').show();
}
});
})(jQuery);
</script>
So, you have to paste in you video ID where I placed: INSERT_YOUR_VIDEO_ID_HERE.
You can find that in the normal Wistia javascript embed code. It's the only weird number code in it.
For <div id="wistia_INSERT_YOUR_VIDEO_ID_HERE" you must have the wistia_ in front of the video ID, so it works.
Hope that helps anyone!
I am using prestashop 1.6 . I want to add google ads in top of the header and bottom in footer. I tried many ways but all are not succeed. Please how can i add script in my prestashop website?
Thanks in advance.
You need to find header.tpl file:
https://github.com/PrestaShop/PrestaShop/blob/develop/themes/default-bootstrap/header.tpl
<head>
{$HOOK_HEADER}
<link rel="stylesheet" href="http{if Tools::usingSecureMode()}s{/if}://fonts.googleapis.com/css?family=Open+Sans:300,600&subset=latin,latin-ext" type="text/css" media="all" />
<!--AdWords Code-->
</head>
Remember to disable CCC options for JS (especially moving JavaScript to the end):
Anything within {literal}{/literal} tags is not interpreted, but displayed as-is
{literal}
<script type="text/javascript">
// ...
</script>
{/literal}
{ldelim} and {rdelim} are used for escaping template delimiters, by default { and }:
<script type="text/javascript">
function foo() {ldelim}
// ...
{rdelim}
</script>
gives:
<script type="text/javascript">
function foo() {
// ...
}
</script>
If you still have a problem you may try to override Media Class:
https://gist.github.com/hereswhatidid/8c8edef106ee95138b03
<p>Some HTML goes here</p>
<script type="text/javascript" data-keepinline="true">
// this script will remain here when rendered
alert( "hello!" );
</script>
<script type="text/javascript">
// this script will be forced to the bottom of the page
alert( "hello again!" );
</script>
Media.php:
<?php
Class Media extends MediaCore
{
public static function deferScript($matches)
{
if (!is_array($matches))
return false;
$inline = '';
if (isset($matches[0]))
$original = trim($matches[0]);
if (isset($matches[1]))
$inline = trim($matches[1]);
/* This is an inline script, add its content to inline scripts stack then remove it from content */
if (!empty($inline) && preg_match('/<\s*script(?!.*data-keepinline)[^>]*>/ims', $original) !== 0 && Media::$inline_script[] = $inline)
return '';
/* This is an external script, if it already belongs to js_files then remove it from content */
preg_match('/src\s*=\s*["\']?([^"\']*)[^>]/ims', $original, $results);
if (isset($results[1]) && (in_array($results[1], Context::getContext()->controller->js_files)
|| in_array($results[1], Media::$inline_script_src)))
return '';
/* return original string because no match was found */
return $original;
}
}
The correct way should be using a module. Also check if the function htmlpurifier is blocking your scripts tags.
A little late, but it is solved by using {literal} //script here {/literal}. It's supposed to be used only if there are curly brackets in your script, but it works.
I'm trying to learn basic HTML and Javascript, and am not sure what is wrong with this code. It is probably a very simple error and I'm sorry if it is. When I try clicking the buttons, Chrome says in the console that "correct" and "incorrect" are not defined, but I have checked the syntax for the functions and I can't see what is wrong. Thanks for your help :)
<!DOCTYPE html>
<html>
<head>
<title>Question 1</title>
</head>
<body>
<p>Q1: What is the height of the Eiffel Tower?</p>
<br>
<script>
function incorrect()
{
document.getElementById("feedback").innerHTML =
"incorrect!
<br>
Next Question";
}
function correct()
{
document.getElementById("feedback").innerHTML =
"Correct!
<br>
Next Question";
}
</script>
<button onclick="incorrect()">767m</buttton>
<br>
<button onclick="incorrect()">442m</button>
<br>
<button onclick="correct()">324m</button>
<br>
<button onclick="incorrect()">278m</button>
<p id="feedback"></p>
</body>
You have confusing ""(double quotes) in the innerHTML strings. Try this:
instead of "q2.htm" use 'q2.htm'
<script>
function incorrect()
{
document.getElementById("feedback").innerHTML =
"incorrect!<br><a href='q2.htm'>Next Question</a>";
}
function correct()
{
document.getElementById("feedback").innerHTML =
"Correct!<br><a href='q2.htm'>Next Question</a>";
}
</script>
If you look at the console log in Chrome (press F12 to enter Developer Tools where you can see the log), you will see an error message “Unexpected token ILLEGAL”. The reason is that you have line breaks inside a JavaScript string, which is not permitted, so the function definitions fail in parsing. Moreover, you are using quotes inside a quoted string, which isn’t permitted either. Use single quotes (') as inner quotes or (in this case) just omit them, e.g.
function incorrect()
{
document.getElementById("feedback").innerHTML =
"incorrect!<br><a href=q2.htm>Next Question</a>";
}
This works:
<script>
function incorrect()
{
document.getElementById("feedback").innerHTML =
"incorrect!<br><a href='q2.htm'>Next Question</a>";
}
function correct()
{
document.getElementById("feedback").innerHTML =
"Correct!<br><a href='q2.htm'>Next Question</a>";
}
</script>
You have to put them on the same line or use concatenation.