jquery find and replace iframe src - javascript

I need to load a url in an iframe on page load (which is working), then grab the whole url of that loaded iframe, then once the url is saved as a variable i want to find any text between "target" and "/" (example url http://www.example.com/user/target09576/index.php i wish to find the 09576) then redirect the whole page using the found text (09576) as part of the redirect url.
I know, bad explanation and not a great solution, but any help would be appreciated.
$(document).ready(function() {
var iframe = $('#wmif');
var url = '<? echo "".$redirectlocation.""; ?>';
iframe.attr('src', url);
var searchurl = iframe.contentWindow.location;
var rep = /(.*target\s+)(.*)(\s+\/.*)/;
var target = searchurl.replace(rep, '$2');
window.location = 'http://example.com/go/index.php?target='+target;
});
well the iframe loads the new location ok, but it doesn't do any of the rest.

I would do it similar to the below, same result but with jQuery and an updated regex
$(document).ready(function () {
var iframe = $('#wmif');
var url = ''<? echo "".$redirectlocation.""; ?>';
iframe.attr('src', url);
//var searchurl = iframe.contentWindow.location; <- Invalid
console.log($('#wmif').contents().get(0).location);
var searchurl = $('#wmif').contents().get(0).location.href;
console.log(searchurl);
var rep = /.*\/target(\w+)\/.*/;
var target = searchurl.replace(rep, '$1');
console.log(target);
window.location = 'http://domain.com/go/index.php?target=' + target;
});

Yet another solution
var matches = /target\s?([^\/]+)/.exec('http://www.domain.com/user/target09576/index.php')
or
var matches = /(?:target\s?)([^\/]+)/.exec('http://www.domain.com/user/target09576/index.php')
console.log(matches[1]); // 09576
Modify regex to this /(?:target)([^\/]+)/ if you don't expect a space between target and the number.

Related

Pass Google Apps Script variable to HTML script

I have a trigger set up in Google Sheets so a URL is automatically opened in a new browser window. This works if the URL is hardcoded. I want the URL to be a variable. How do I pass the URL variable from Apps Script to HTML script? I'm a novice coder so please explain like I'm 5.
This function works if the URL is text like 'https://www.google.com'
function openMap() {
var maplink = SpreadsheetApp.getActiveSheet().getRange("B2").getValues();
var js = "<script>window.open('https://www.google.com', '_blank', 'width=800, height=600');google.script.host.close();</script>";
var html = HtmlService.createHtmlOutput(js)
.setHeight(10)
.setWidth(100);
SpreadsheetApp.getUi().showModalDialog(html, 'Now loading.');
}
This function does not if the URL is a variable (I checked the maplink value and its a valid URL)
function openMap() {
var maplink = SpreadsheetApp.getActiveSheet().getRange("B2").getValues();
var js = "<script>window.open('maplink', '_blank', 'width=800, height=600');google.script.host.close();</script>";
var html = HtmlService.createHtmlOutput(js)
.setHeight(10)
.setWidth(100);
SpreadsheetApp.getUi().showModalDialog(html, 'Now loading.');
}
You can join strings in the following manner:
...
var url = 'https://www.google.com';
var js = "<script>window.open('" + url + "', '_blank', 'width=800, height=600');google.script.host.close();</script>";
...
Basically
you close the first part of your hardcoded string by closing the quotes
add the dynamical variabl with +
continue the hardcoded string by appending the second part with + and opening the quotes again
I hope this is clear!

Using Auth function in script tag (Laravel)

guys, I am trying to use the Auth function in my script tag which is inside a blade file but it seems like I am getting a yellow warning when I checked their source in google. Is it possible to use the Auth function in a script tag or is there an alternative approach for this? Anyone able to help? thanks in advance.
Here my snippets of code in the script tag
<script type="text/javascript">
$('button').click(function(){
//CONST DATA
const prefix = "QATS";
const url = "http://qr5.dev.ificonsultancy.net/api/sendData";
//Extract Data
var lecturer_id = Auth::user()->id; //HERE IS THE ERROR
var faculty_code = $( "#Faculty" ).val();
var programme_code = $( "#Programme" ).val();
var class_code = $("#Class").val();
//Location Coordinates
var gps_school_lng = "101.5597904";
var gps_school_lat = "3.0923526";
//URL Concatenation
var urlContent = prefix.concat("|",url,"|",lecturer_id,"|",faculty_code,"|",programme_code,"|",class_code,"|",gps_school_lat,"|",gps_school_lng);
//Add src to #qrImg
$("img#qrImg").attr('src',"https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl="+urlContent+"&choe=UTF-8");
console.log("pass");
});
</script>
Yes, just use the mustache syntax:
var lecturer_id = '{{ Auth::user()->id; }}'

How can add parameters to href url when click on tag a?

I want From home page send parameter id to carts page:
This is current code, but it not woking:
detail
<script>
function addParam()
{
var url = window.location.href;
var listId = localStorage.getItem('IDs', '');
url=url+ "?id=" +listId;
window.location.href = url;
}
</script>
How can add parameters to href url when click on tag a?
Either just override it by grabbing the element's (here done by passing this to the function) href
detail
<script>
function addParam(el)
{
var listId = localStorage.getItem('IDs', '');
window.location.href = el.href + "?id=" +listId;
}
</script>
Or append it to the element's href
detail
<script>
function addParam(el)
{
var listId = localStorage.getItem('IDs', '');
el.href += "?id=" +listId;
}
</script>

Call one function after another to get the proper url

Hi I am working in grails ..
<g:link name="searchLink" controller="MRController"
action="ExcelExport">TEST GRAILS</g:link>
<script>
$(function() {
$('a[name="searchLink"]').bind('click', function() {
$(this).attr('href', $(this).attr('href') + '?startdate=' + start().StartDate +'&enddate=' + start().Enddate);
})
})
function start(){
var StartDate = $("#startDate").val();
var Enddate= $("#endDate").val();
return {StartDate:StartDate,Enddate:Enddate}
}
</script>
I have trying to get the following url
http://localhost:8077/Myproject/MRController/ExcelExport
?startdate=2017-05-21&enddate=2017-05-23
But when i click the link again i get the following url instead i want the link with new startdate and enddate value
http://localhost:8077/Myproject/MRController/ExcelExport?
startdate=2017-05-21&enddate=2017-05-23?startdate=2017-05-21&enddate=2017-05-23
Edited
I have tried with the following script to clear the parameters in the first call but i don't know how to make the script run one after another means first the url call work then the parameters will clear through the function
<script>
function refineUrl()
{
//get full url
var url = window.location.href;
//get url after/
var value = url.substring(url.lastIndexOf('/') + 1);
//get the part after before ?
value = value.split("?")[0];
return value;
}
</script>
Don't change the original href attribute in your JS-code:
$('a[name="searchLink"]').bind('click', function() {
document.location = $(this).attr('href') + '?startdate=' + start().StartDate +'&enddate=' + start().Enddate;
})

Get Meta Description from URL using jQuery or javascript

i want to get the meta description of the parent page from an iframe, what i did uptill now is that i get the url of the parent page, pass that url to jquery and try to get the meta description but it doesn't work, my code is as follows
<script type="text/javascript">
function addToInterest() {
var URL = parent.window.location;
var Title = parent.document.getElementsByTagName("title")[0].innerHTML;
var MetaDescription = "";
var Img_Src = "";
var metaDesc = $.get('http://myURL.com', function (data) {
MetaDescription = $(data).find('meta[name=description]').attr("content");
Img_Src = $(data).find('link[rel=image_src]').attr("href");
});
alert(MetaDescription);
alert(Img_Src);
}
</script>
But in both alerts, it shows nothing.. i have already tried the methods told here
but did not successfull.
any sample code please....
Regards:
Mudassir
$.get is asynchronous. Both your alerts executed just after $.get call, but at this moment HTTP request can be still in progress. You need to move your alerts inside of callback function:
<script type="text/javascript">
function addToInterest() {
var URL = parent.window.location;
var Title = parent.document.getElementsByTagName("title")[0].innerHTML;
var MetaDescription = "";
var Img_Src = "";
var metaDesc = $.get('http://myURL.com', function (data) {
MetaDescription = $(data).find('meta[name=description]').attr("content");
Img_Src = $(data).find('link[rel=image_src]').attr("href");
alert(MetaDescription);
alert(Img_Src);
});
}
</script>
Also note, what your code will hit Same Origin Policy. By default you can't dynamically load resources, placed on other host, than you script.

Categories