google custom search engine multiple parameters query - javascript

Sry, for unclear title to the question, I hope someone will correct it to something more precise.
The situation:
I am using a google custom search engine which is set to search only two sites and display results only.In control panel of GSE when I go to advanced tab in search features, i can specify query parameter that it will look for. after I am done i get a javascript code to paste in my page's code.
I believe that this javascript code listens to $_GET['q'] variable and then produce results based on that.
Problem :
I have four field in a page and a php form set up to display result from the other sites, the end result is the URL with four get variables like $_GET['q1'] to $_GET['q4']. I don't have a $_GET['q'] in the page or anywhere else.
What I tried:
I just can't make the earlier form to print another or a combined get parameter as $_GET['q']. i'll have to add a lot of JS and a hidden input field i guess.
I tried setting $_GET['q'] in php as $_GET['q']=$_GET[q1]; and then adding the javascript for custom search engine.
I tried adding a link to update or set GET anything but this doesn't seem to work.
I think the reason is maybe lack of knowledge about how GSE gets a keyword,search and returns result, and lack of knowledge about $_GET parameter.
Question:
Anyway I can set $_GET['q'] in my php script(dynamically?) that google custom search engine javascript will understand and work fluently ??
Is there any other better way to do this.
This doesn't need me to show code but here is some code that may be relevant to the question :
GOOGLE CUSTOM JAVASCRIPT
<script>
(function() {
var cx = '010474608308939830468:6s3kklfffuu';
var gcse = document.createElement('script');
gcse.type = 'text/javascript';
gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//cse.google.com/cse.js?cx=' + cx;
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(gcse, s);
})();
</script>
<gcse:searchresults-only></gcse:searchresults-only>
And here is my php approach :
$_GET['q'] = $_GET['q1'] ;
any help will be greatly appreciated and hope you can also clear out on the functioning of GSE for fellow readers. Thanks, in advance

So I was unclear from your question if you need to search ALL of the queries, or if the user is only going to fill in one of the forms. If the user fills in only one form, this should work - using PHP to check which query variable is used, and then having the CSE look at that parameter.
<?php
if (isset($_GET['q1']) && $_GET['q1'])
{$param = 'q1';
} elseif (isset($_GET['q2'])) {
$param = 'q2';
}
echo "<gcse:search queryParameterName='".$param."'></gcse:search>";
?>
If you need to concatenate all your queries into one, your best bet would probably be to rewrite the URL using htaccess before it hits the page, so that all the parameters are concatenated into one query parameter before the page loads. Then set the queryParameterName to whatever you have called that variable.
If you don't have access to creating a rewrite rule on your server, you could explore this answer: changing the query string without reloading the page. Just do that ahead of the tag, which evaluates the query string to figure out what you are searching for.

Related

Remove last 3 letters of div (hidde also in browser page source)

this is my HTML
<div id="remove">Username</div>
and this is my JS code
function slice() {
var t = document.getElementById("remove");
t.textContent = t.textContent.slice(0, -3);
}
slice();
Username load from foreach
{foreach from=$last_user item=s}
{$s.date}
{$s.username}
{/foreach}
This code working and remove 3 letter but when right click on browser and look at page sources i can see "Username" !
I need remove three letter because of privacy and security .
something like
*** name or usern ***
Thank for help me !
The only secure way to make sure the client can't see a particular piece of information is to never send it to the client in the first place. Otherwise, there will always be a way for the client to examine the raw payloads of the network requests and figure out the information they aren't supposed to know.
You'll need to fix this on your backend - either hard-code in
<div id="remove">Usern</div>
or, for a more dynamic approach, use a template engine (or whatever's generating the HTML) and look up how to change strings with it. For example, in EJS, if user is an object with a username property, you could do
<div id="remove"><%= user.username.slice(0, -3) %></div>
Changing the content only with client-side JavaScript will not be sufficient, if you wish to keep some things truly private.
With Smarty, you can define a modifier that takes a string and returns all but the last three characters of it.
function smarty_modifier_truncate_three($string)
{
return substr($string, 0, -3);
}
and then in your template, replace
{$s.username}
with
{$s.username|truncate_three}
If you want only the first three characters, it's easier because you can use the built-in truncate.
{$s.username|truncate:3}
JS doesn't change the source, it can only change the DOM, so what you can do is to keep the element empty and add a value to it using js, but don't forget that js runs on the client's side so its better here to send the string from the server without the last 3 characters.

How do I get document.getElementsByTagName('').innerHTML to make text between 2 tags?

I'm trying to use JavaScript to include a footer on several webpages, so if I want to change the footer, I only have to change it in one place. PHP is not available on this server and neither are server side inserts (SSI), but Perl, Python, and Tcl are available. I have been trying with document.getElementsByTagName('footer').innerHTML = "text"; but it doesn't produce text. I copied this code from dev.mozilla, and it tells me how many tags I have:
var footer = document.getElementsByTagName('footer');
var num = footer.length;
console.log('There is ' + num + ' footer in this document');
So, I don't know what's wrong with the innerHTML script. I also tried with paragraph tags and got the same results in both cases.
I reccoment using textContent instead. Se why here.
To see how it works, paste the following into your browser console while you're on StackOverflow and hit enter.
document.querySelector('.site-footer').textContent = 'Custom footer content.'
note: use querySelector with a class instead of getElementByTagName
Cheers! 🍻
Before asking this question, I had searched for Python includes without any luck, so I stopped there, but after asking this question, I thought that I should search for Perl/Ruby includes. Today, I found out that I can use the Perl use function, so I could study that and try to implement it although I am completely new to Perl. Ruby also appears capable, perhaps even more. I have no experience with Ruby either, but maybe I should start there.
I just figured out that getElementsByTagName() results in an array, so I have to refer to the footer's index with [0]:
var footerTags = document.getElementsByTagName('footer');
footerTags[0].innerHTML = "test";

How to use javascript split in Qualtrics

Very new to Javascript and have been searching the webs for assistance, but haven't quite found a solution.
I am attempting to use javascript to split/remove the output of a particular field. The data in the survey is being pulled from our school's database after a user logs in to the survey via shibboleth. All the information is being displayed, so that part works, but one particular field is appending an email address (#email.com) to a field.
I want to omit this part from being displayed. Either my javascript is incorrect or the javascript is not being loaded/read. The javascript code was borrowed from a colleague and it works on his surveys, but he has a lot of other things going on in his survey and this works for him.
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
var iid = "${e://Field/theUTIID}";
var split_array = iid.split("#",1);
var eid = split_array[0];
Qualtrics.SurveyEngine.setEmbeddedData('theUTIID', eid);
});
Qualtrics.SurveyEngine.addOnReady(function()
{
/*Place your JavaScript here to run when the page is fully displayed*/
var iid = "${e://Field/theUTIID}";
var split_array = iid.split("#",1);
var eid = split_array[0];
Qualtrics.SurveyEngine.setEmbeddedData('theUTIID', eid);
});
I have this in both the Onload and OnReady for testing. Doesn't matter if I have this is one location or the other, I am not getting the desired results.
I only have one question on the survey (it's just a test survey) and so the javascript code is with the first and only question.
Survey Question has the following in a text entry. Again, output is displayed, but need the #email.com to removed from the EID field.
The code looks correct (other than it only needs to be in one or the other function). I'm guessing it isn't a problem with the code, but where you are trying to pipe the embedded variable. The JavaScript above has to be attached to a question on a separate page before the place where you want to pipe it.
Add a page break, then pipe theUTIID into a question on the next page.

in JavaScript searching a string for end of line

My problem involves using JavaScript in a gmail sheet to search emails using getRawContent.
I've been using indexOf to get a particular data from the string, I'm able to find the starting point for the data I want to use.
For example var startxmailer = rawmail.indexOf("X-Mailer: ",0);, but I'm unable to identify or locate end of line/carriage return to give me an end point to use sub-string to get the data for example var allxmailer = rawmail.substring(endxmailer,test1);.
I've tried indexOf(/[\n,\r]/,0) and (/\r,\n,|\r|,\n,\$,\O,\x/).
I've put the section of text into a variable in the script and used the above to locate end of line/carriage return but not been to successful any assistance would be much appreciated as I would like to be able to different section of data from a email in Raw Content.
Try this:
var xmailer = (rawmail.match(/X-Mailer: (.*)/i) || [])[1];
This will get the content of the X-Mailer header into the xmailer variable, or undefined if no such header was found. This works because . explicitly matches everything that isn't a new line.

Remove prefix with unknown characters in JavaScript

I have a web page that the title is changed from 'Pagename' to '(1) Pagename' when there is an update on the page. That number increments to 50 each time there is a new update and then is maxed out showing '(50+) Timeline'.
When logging page views, Google Analytics shows the '(n) Pagename', which I don't want. So I found out how to manually change to logged page title, _gaq.push(["_set", "title", 'new title']);.
So my question is, how do I most efficiently remove the (1-50)/(50+) prefix and just get 'Pagename'? Is regex best for this?
This is what I'm using based on the answer from Ross:
var window_title = window.title.replace(/^\(\d+\+?\)\s/, '');
_gaq.push(["_set", "title", window_title]);
Yes, RegEx can do that.
window.title.replace(/^\(\d+\+?\)\s/, '');
Of course it depends on what software your site is using as perhaps it would be possible to just output the page title without that prefix in the relevant part of the template. So echoing that directly into the Google Analytics tag. But I think the above javascript is probably the easier solution to implement.

Categories

Resources